我正在尝试创建一个为标题 (h1-h6) 创建自动编号的 javascript 函数(我可以在多个项目中重复使用)。
我目前可以使用以下内容通过 css 来完成:
body { counter-reset: h1; }
h1 { counter-reset: h2; }
h2 { counter-reset: h3; }
h3 { counter-reset: h4; }
h4 { counter-reset: h5; }
h5 { counter-reset: h6; }
h1:before {
content: counter(h1,decimal) ". ";
counter-increment: h1;
}
h2:before {
content: counter(h1, decimal) "."
counter(h2, decimal) ". ";
counter-increment:h2;
}
h3:before {
content: counter(h1, decimal) "."
counter(h2, decimal) "."
counter(h3, decimal) ". "
counter-increment:h3;
}
h4:before {
content: counter(h1, decimal) "."
counter(h2, decimal) "."
counter(h3, decimal) "."
counter(h4, decimal) ". ";
counter-increment:h4;
}
h5:before {
content: counter(h1, decimal) "."
counter(h2, decimal) "."
counter(h3, decimal) "."
counter(h4, decimal) "."
counter(h5, decimal) ". ";
counter-increment:h5;
}
h6:before {
content: counter(h1, decimal) "."
counter(h2, decimal) "."
counter(h3, decimal) "."
counter(h4, decimal) "."
counter(h5, decimal) "."
counter(h6, decimal) ". ";
counter-increment:h6;
}
它创建了这种格式:
1. Heading number 1
1.1. Heading level 2
1.1.1. Heading level 3
2. Heading number 2
3. Heading number 3
2.1. Heading level 2
2.2. Heading level 2
2.2.1. Heading level 3
但我想将其转换为 JS 函数(并删除 CSS 药水),以便在某些页面上我可以拥有:
if( typeof mbopts !== 'undefined' && mbopts.length > 0 ) {
var mbopts = {
levels: Int, // [1-6 being H1-H6]
separator: String, // decimal, hyphen, brace -> .,)
startAt: Int, // default: 1, but what the begin numbering at
};
}
$('#main').mbheaders(mbopts);