列表的正确书写方法

发布于 2024-07-09 22:04:16 字数 864 浏览 7 评论 0原文

这是我思考了一段时间的事情,因为我在实践中看到了两者的使用。

方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总攻大人 2024-07-16 22:04:16

方法 2 不是有效的 HTML。 不允许 OL 作为另一个 OL 元素的直接子元素。 OL 中仅允许使用 LI

此外,由于缩进,第 2 项的子列表的成员资格仅对人类读者可见。 使用LI进行缩进,看起来内部列表本身就是第三项。 我可能期望这样的渲染,有两个数字:

1. List item 1
2. List item 2
3. 1. List item 3
4. List item 4

方法 1 是最佳选择。

Method 2 is not valid HTML. OL is not allowed as a direct child of another OL element. Only LI is allowed in an OL.

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 LIs, and it appears as though the inner list is itself the third item. I might expect a rendering like this, with two numbers:

1. List item 1
2. List item 2
3. 1. List item 3
4. List item 4

Method 1 is the way to go.

探春 2024-07-16 22:04:16

方法1是正确的。

Method 1 is correct.

十年九夏 2024-07-16 22:04:16

HTML 4.01 仅允许 OL 中的 LI 项。 然而,LI 的结束标记是可选的。 这意味着这相当于您的方法 1:

<ol>
   <li>List item 1
   <li>List item 2
      <ol>
        <li>List item 3
      </ol>
   <li>List item 4
</ol>

对于读者来说,结束 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:

<ol>
   <li>List item 1
   <li>List item 2
      <ol>
        <li>List item 3
      </ol>
   <li>List item 4
</ol>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文