我读了related帖子,但没有找到适用于 IE 的解决方案,因此我请求针对此问题的 jQuery 解决方案:
我有一些像这样的嵌套分层标题
<h1> heading 1</h1>
<h2> subheading 1</h2>
<h1> heading 2</h1>
<h2> subheading 1</h2>
<h2> subheading 2</h2>
我需要像这样的一些自动标题输出:
1. heading 1
1.2 subheading 1
2. heading 2
2.1. subheading 1
2.2. subheading 2
有没有办法在 IE6+ 中使用 jQuery 或类似工具来实现这一点?
最佳答案
这是我的看法:
var segments = [];
$(':header').each(function() {
var level = parseInt(this.nodeName.substring(1), 10);
if(segments.length == level) {
// from Hn to another Hn, just increment the last segment
segments[level-1]++;
} else if(segments.length > level) {
// from Hn to Hn-x, slice off the last x segments, and increment the last of the remaining
segments = segments.slice(0, level);
segments[level-1]++;
} else if(segments.length < level) {
// from Hn to Hn+x, (should always be Hn+1, but I'm doing some error checks anyway)
// add '1' x times.
for(var i = 0; i < (level-segments.length); i++) {
segments.push(1);
}
}
$(this).text(segments.join('.') + '. ' + $(this).text());
});
关于jquery - 使用 jQuery 自动编号标题 H1-H6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5127017/