这是一个适用于所有浏览器的示例。纯CSS方法可在实际的浏览器中使用(即,除IE6 / 7之外的所有内容),而jQuery代码将覆盖不受支持的代码。它具有SSCCE的风格,您无需更改即可复制'n'paste'n'run。
SO question 2729927$(document).ready(function() {
if ($('ol:first').css('list-style-type') != 'none') { /* For IE6/7 only. */
$('ol ol').each(function(i, ol) {
ol = $(ol);
var level1 = ol.closest('li').index() + 1;
ol.children('li').each(function(i, li) {
li = $(li);
var level2 = level1 + '.' + (li.index() + 1);
li.prepend('' + level2 + '');
});
});
}
});
html>/**/body ol { /* Won't be interpreted by IE6/7. */
list-style-type: none;
counter-reset: level1;
}
ol li:before {
content: counter(level1) ". ";
counter-increment: level1;
}
ol li ol {
list-style-type: none;
counter-reset: level2;
}
ol li ol li:before {
content: counter(level1) "." counter(level2) " ";
counter-increment: level2;
}
ol li span { /* For IE6/7. */
margin: 0 5px 0 -25px;
}
- first
- second
- second nested first element
- second nested second element
- second nested third element
- third
- fourth