我正在尝试使用HTML在文件中编写目录。我的目录中出现的编号存在缺陷。如何修改以下CSS
和HTML
,以便我Output
中的第四个项目符号点是4
而不是3.4
?
ol {
counter-reset: item;
}
ol li {
line-height: 30px;
}
ol ol li {
line-height: 20px;
}
li {
display: block;
}
li:before {
content: counters(item, ".")" ";
counter-increment: item;
}

<ol>
<li>
<a href="#lorem_ipsum">lorem ipsum</a>
</li>
<li>
<a href="#set">Set</a>
</li>
<li>
<a href="#title">Title</a>
</li>
<ol>
<li>
<a href="#Class">Class</a>
</li>
<li>
<a href="#Something">Something</a>
</li>
<li>
<a href="#action">Action</a>
</li>
</ol>
<li>
<a href="#table_of_references">Table of References</a>
</li>
</ol>
&#13;
输出
1. Lorem Ipsum
2. Set
3. Title
3.1. Class
3.2. Something
3.3. Action
3.4. Table of References
答案 0 :(得分:5)
正如评论所说(@Jon P):您的HTML无效。您不能将ol
作为ol
的直接后代,而是需要将其li
包裹起来。
要正确嵌套列表,只需将第3个li中的子ol
移动,请查看以下示例:
参考:https://developer.mozilla.org/en/docs/Web/HTML/Element/ol
ol {
counter-reset: item;
}
ol li {
line-height: 30px;
}
ol ol li {
line-height: 20px;
}
li {
display: block;
}
li:before {
content: counters(item, ".")" ";
counter-increment: item;
}
&#13;
<ol>
<li>
<a href="#lorem_ipsum">lorem ipsum</a>
</li>
<li>
<a href="#set">Set</a>
</li>
<li>
<a href="#title">Title</a>
<ol>
<li>
<a href="#Class">Class</a>
</li>
<li>
<a href="#Something">Something</a>
</li>
<li>
<a href="#action">Action</a>
</li>
</ol>
</li>
<li>
<a href="#table_of_references">Table of References</a>
</li>
</ol>
&#13;
答案 1 :(得分:1)
您在标题后过早关闭LI标签 - 将其移至OL区域下方,如下所示:
<ol>
<li>
<a href="#lorem_ipsum">lorem ipsum</a>
</li>
<li>
<a href="#set">Set</a>
</li>
<li>
<a href="#title">Title</a>
<ol>
<li>
<a href="#Class">Class</a>
</li>
<li>
<a href="#Something">Something</a>
</li>
<li>
<a href="#action">Action</a>
</li>
</ol>
</li>
<li>
<a href="#table_of_references">Table of References</a>
</li>
</ol>