HTML DOM 快速導覽 - 樣式設定的屬性 counterIncrement

CSS 樣式表 (cascading style sheets) 的 counterIncrement 性質 (property) 可控制 HTML 元素 (element) 編號的遞增,亦可由 JavaScript 程式取得元素物件,再用 style 的屬性 (attribute) counterReset 進行設定。



counterReset 需要提供兩個值
"identifier number"


identifier 為相關的識別字, number 則是編號遞增數字。


舉例如下
function run00() {
    var d = document.getElementById("demo");
    d.style.counterIncrement = "section 6";
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:style073.js
    功能:示範 JavaScript 程式 
    作者:張凱慶
    時間:西元 2011 年 8 月 */


此例將 id 屬性為 demo 的元素設定成 section 6
var d = document.getElementById("demo");
d.style.counterIncrement = "section 6";


我們以下面的 HTML 文件載入
<html>
<head>
<title>網頁標題</title>
<script src="style073.js" type="text/javascript"></script>
<style>
body {
    counter-reset: chapter 3;
}
h2 {
    counter-reset: section;
}
h3 {
    text-indent: 20px;
}
h2:before {
    counter-increment: chapter 2;
    content: "Chapter " counter(chapter) " ";
    
}
h3:before {
    counter-increment: section;
    content: "Section " counter(chapter) "." counter(section) " ";
}
</style>
</head>
<body>
<input type="button" value="RUN" onclick="run00();">
<h2>A prison for your mind.</h2>
<h3 id="demo">Believe the unbelievable.</h3>
<h3>There is no spoon.</h3>
<h3>Choice. The problem is choice.</h3>
<h3>Because I choose to.</h3>
<h3>Everything that has a beginning has an end.</h3>
</body>
</html>

<!-- 《程式語言教學誌》的範例程式
     http://pydoing.blogspot.com/
     檔名:style073.html
     功能:示範 JavaScript 程式 
     作者:張凱慶
     時間:西元 2011 年 8 月 -->


瀏覽器 (browser) 開啟如下



點擊 RUN 之後如下



中英文術語對照
CSS 樣式表cascading style sheets
性質property
元素element
屬性attribute
瀏覽器browser


您可以繼續參考
CSS 範例

樣式設定物件 style


相關目錄
HTML DOM 快速導覽
JavaScript 教材
首頁


參考資料
https://developer.mozilla.org/en/DOM/CSS
https://developer.mozilla.org/en/CSS/counter-increment

沒有留言: