列表的正确书写方法
这是我思考了一段时间的事情,因为我在实践中看到了两者的使用。
方法 1
<ol>
<li>List item 1</li>
<li>List item 2
<ol>
<li>List item 3</li>
</ol>
</li>
<li>List item 4</li>
</ol>
这对我来说在语义上似乎是正确的,因为子列表是该列表项的子列表,但它不是很干净(List Item Content
)。
方法 2
<ol>
<li>List item 1</li>
<li>List item 2</li>
<ol>
<li>List item 3</li>
</ol>
<li>List item 4</li>
</ol>
更清晰,但不能直接理解该列表是项目 2 的子列表(尽管通过人类推理可以理解)。
顺便说一句,这纯粹是语义标记的问题,两种方法在页面上呈现相同的内容。
所以,你觉得怎么样? 尽可能使用来源是更好的选择,但个人使用也很好。 我注意到 MediaWiki 的分级目录正在使用方法 1,这让我相信这是正确的用法。
This is something I've pondered over for a while, as I've seen both used in practise.
Method 1
<ol>
<li>List item 1</li>
<li>List item 2
<ol>
<li>List item 3</li>
</ol>
</li>
<li>List item 4</li>
</ol>
This seems semantically correct to me, since the sub-list is a sub-list of that list item, however it isn't very clean (List Item Content<list right next to it>
).
Method 2
<ol>
<li>List item 1</li>
<li>List item 2</li>
<ol>
<li>List item 3</li>
</ol>
<li>List item 4</li>
</ol>
Cleaner, but it's not directly understandable the list is a sub-list of item 2 (although it's understandable by human inference).
This is purely a concern for semantic markup by the way, both methods present the same content on the page.
So SO, what do you think? Sources where possible are preferable but personal use is good too. I notice that MediaWiki's heirachial TOCs are using Method 1 which encourages me to believe that's the correct use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法 2 不是有效的 HTML。 不允许
OL
作为另一个OL
元素的直接子元素。OL
中仅允许使用LI
。此外,由于缩进,第 2 项的子列表的成员资格仅对人类读者可见。 使用
LI
进行缩进,看起来内部列表本身就是第三项。 我可能期望这样的渲染,有两个数字:方法 1 是最佳选择。
Method 2 is not valid HTML.
OL
is not allowed as a direct child of anotherOL
element. OnlyLI
is allowed in anOL
.Also, the membership of the sublist to item 2 is only apparent to a human reader because of the indentation. Make the indentation even with the
LI
s, and it appears as though the inner list is itself the third item. I might expect a rendering like this, with two numbers:Method 1 is the way to go.
方法1是正确的。
Method 1 is correct.
HTML 4.01 仅允许 OL 中的 LI 项。 然而,LI 的结束标记是可选的。 这意味着这相当于您的方法 1:
对于读者来说,结束 LI 的位置有些模糊 - 尽管规范明确表示它将在结束 OL 之后。
XHTML 1.1 具有相同的限制,但是强制使用结束 LI 使其明确。
HTML 4.01 only allows LI items in an OL. The closing tag of LI, however, is optional. Which means this is equivalent to your method 1:
It is somewhat ambiguous to a reader where the closing LI's are - though the spec is clear that it would be after the closing OL.
XHTML 1.1 has the same restrictions, but forces the use of the closing LI to make it explicit.