【问题标题】:Is it semantically correct in HTML to markup a list with only a single list item?在 HTML 中仅使用单个列表项标记列表在语义上是否正确?
【发布时间】:2012-05-31 13:49:54
【问题描述】:

将 HTML 中的 ul 标记为只有一个列表项的另一个嵌入式 ul 在语义上是否正确?

例如,我有一个ul 和几个lis,其中一个lis 有一个嵌入的ul 和一个li

<ul>
  <li>Example LI 1
    <ul>
        <li>Example LI 1a</li>
    </ul>
  </li>
  <li>Example LI 2</li>
  <li>Example LI 3</li>
</ul>

【问题讨论】:

  • 为什么你认为它不正确?
  • 另外,您的主要问题(“标题”,如果您愿意的话)和您实际提出的第一个问题是相反的。一个问它在语义上是否正确,另一个问它是否在语义上不正确。您希望人们回答哪个?
  • @icabod,已修复以避免混淆。

标签: html html-lists semantic-markup


【解决方案1】:

当然。列表不是由数量定义的。它是由语义定义的。因此,如果只有一项适用于列表的目的,则列表只能包含一个元素。例如,我今天只使一台计算机崩溃,因此该列表只有一个元素长。

【讨论】:

    【解决方案2】:

    是的,如果使用得当,一个包含单个项目的列表在语义上是正确的,即使它确实感觉有点奇怪(如果只有一个项目,它就不是一个真正的列表,因为根据定义,一个列表是,嗯, list 项目​s,否则它只是一个项目)。

    在您提供的示例中,这些项目是占位符并且没有任何意义,因此很难判断它是否适用。它是否正确取决于为什么你把它放在一个子项中。如果它确实是一个列表项,那么拥有一个单项子列表是完全合理的,特别是如果有其他包含多个项的子列表。那样的话,意思就很清楚了。例如,以下是可以的:

    <h1>Auto Insurance Customers</h1>
    <ul>
    
      <li>
        <strong>#1234</strong>
        <ul>
          <li>2003 Ford Focus</li>
          <li>1998 Ford Escort</li>
        <ul>
      </li>
    
      <li>
        <strong>#2468</strong>
        <ul>
          <li>1999 Lamborghini Diablo VT Roadster</li>
        </ul>
      </li>
      …
    
    </ul>
    

    这个例子没有错,因为一个客户拥有一辆车而其他人可能拥有更多是完全合理的。


    另一方面,如果您制作单项子列表的原因只是为了创建一个缩进来偏移和突出列表项的一部分,那么这是不正确的。

    例如,假设您有一个文本段落列表。在其中一个段落中,您有一段需要注意的段落,并且您想缩进和偏移它。虽然将它放在列表中会起作用,但这是不正确的,因为您将样式与结构混合在一起。

    完成此操作的正确方法是将部分包装在另一个标记中(如&lt;blockquote&gt;、样式&lt;div&gt; 等)并将其保留在该列表项的常规流程中。在以下示例中,需要突出显示列表项之一的一部分。将其放入(单项)列表中是错误的做法:

    <h1>Blog posts or something…</h1>
    <ul>
    
      <li>
        <strong>Foo</strong>
        <p>Some long paragraph about Foos, what they do, how they work, etc.</p>
        <p>More information about Foos and where they originated.</p>
        <p>Instructions on care and feeding of Foos.</p>
      </li>
    
      <li>
        <strong>Bar</strong>
        <p>Detailed description of local watering holes for lawyers.</p>
        <p>Anecdotes about experiences of drinking & lawyering.</p>
    
        <!-- This section is to be highlighted and offset to draw attention to it from the rest of this list item. -->
        <ul>
          <li>
            <p>Highlighted account of the subsequent problems and what happened that one strange night.</p>
          </li>
        </ul>
    
        <p>Summary of what to not do when out drinking with lawyers.</p>
      </li>
    
      <li>
        <strong>Baaz Luhrmann</strong>
        <p>A bunch of stuff about a brass-skinned movie director who made a widely-panned film adaptation of a Shakespeare play who turns to stone and traps your sword when he dies.
      </li>
    
    </ul>
    


    相反,您应该为此使用正确的标签。它不仅在语义和结构上正确,而且更清晰,甚至缩小了页面的大小:

    <style…>
      p.highlight {
        margin : 20px 50px;
        width  : 150px;
      }
    </style>
    
    …
      <li>
        <strong>Bar</strong>
        <p>Detailed description of local watering holes for lawyers.</p>
        <p>Anecdotes about experiences of drinking and lawyering.</p>
    
        <!-- This section is to be highlighted and offset to draw attention to it from the rest of this list item. -->
        <p class="highlight">
          Highlighted account of the subsequent problems and what happened that one strange night.
        </p>
    
        <p>Summary of what to not do when out drinking with lawyers.</p>
      </li>
    

    【讨论】:

    • (我今天遇到了这个问题,并决定在单项列表上查找意见。看到这个页面后我突然想到了解决方案,所以我想我会分享它。我猜泽尔德曼的讨论停滞不前,阅读他的书得到了回报。☺)
    • 这样使用blockquote 是不正确的:“表示引用从另一个来源的部分”。此外,您通常不希望在列表项的 inside 中使用标题(不使用分段元素),因为下一个列表项的开头仍然在前一个列表的标题范围内项目(请参阅li 的最后一条注释)。
    • @unor,这只是一个人为的例子;关键是不要使用列表标签进行样式设置。在任何情况下,我都用strongs替换了标题,用带样式的段落替换了blockquote(尽管使用blockquote没有任何问题,特别是在实际的非示例使用中,突出显示的段落可能是实际的引用或摘录——就像今天下午我的情况一样)。
    • 嗯,你写的是“需要注意的段落”→“实现这一点的正确方法是将部分包装在&lt;blockquote&gt;中”。这表明blockquote 应该在任何需要引起注意的情况下使用。 – 感谢您的编辑。
    • 实际上,在前面提到的 Zeldman 文本的 pg 358 中,他提到了滥用块引用来格式化。 :-D
    【解决方案3】:

    根据dictionary.com,list 是一组有意义的项目系列,series 是一组相似或相关的项目。 (oxforddictionaries.com 和 thefreedictionary.com 的结果相似。)任何仅包含一项的内容都不能包含有意义的分组或内容之间的相似性或相关性。因此,对于单项列表,标记的语义与内容的语义不匹配。

    单项列表似乎也不符合人们说“列表”时的意思。 (关于这一点,当代词典更侧重于记录常用用法而不是正确用法(这就是 OED 中出现“bling”的原因)。

    此外,即使技术上没有错误,将这样一个简单的语句标记为列表而不是段落似乎也没有太大的编辑价值。在我看来,以下三个示例中的第一个对用户来说是最容易解析和理解的。

    <p>XYZ is the only computer that crashed.</p>
    
    <p>
         The computer that crashed:
         <ul><li>XYZ</li></ul>
    <p>
    
    <ul>
         <li>
              The computer that crashed:
              <ul><li>XYZ</li></ul>
         </li>
    <ul>
    

    【讨论】:

      猜你喜欢
      • 2011-09-02
      • 1970-01-01
      • 2022-01-02
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      相关资源
      最近更新 更多