使用 HTML5 语义标记搜索结果列表

发布于 2024-09-10 04:40:26 字数 1782 浏览 5 评论 0 原文

如果您只需要一些有用的东西,那么制作搜索结果列表(就像在 Google 中一样)并不难。然而现在,我想利用 HTML5 语义的优势来完美地做到这一点。目标是定义标记搜索结果列表的事实上的方式,该列表可能被任何未来的搜索引擎使用。

对于每个点击,我想

  • 通过增加数字来排序它们
  • 显示可点击的标题
  • 显示简短的摘要
  • 显示其他数据,例如类别,发布日期和文件大小

我的第一个想法是这样的:

<ol>
  <li>
    <article>
      <header>
        <h1>
          <a href="url-to-the-page.html">
            The Title of the Page
          </a>
        </h1>
      </header>
      <p>A short summary of the page</p>
      <footer>
        <dl>
          <dt>Categories</dt>
          <dd>
            <nav>
               <ul>
                  <li><a href="first-category.html">First category</a></li>
                  <li><a href="second-category.html">Second category</a></li>
                </ul>
            </nav>
          </dd>
          <dt>File size</dt>
          <dd>2 kB</dd>
          <dt>Published</dt>
          <dd>
            <time datetime="2010-07-15T13:15:05-02:00" pubdate>Today</time>
          </dd>
        </dl>
      </footer>
    </article>
  </li>
  <li>
    ...
  </li>
  ...
</ol>

我对 < 不是很满意

  • 内的;article/>。首先,搜索结果本身并不是一篇文章,而只是一篇非常简短的摘要。其次,我什至不确定您是否可以将文章放入列表中。
  • 也许

    标签比
    更合适,但我不知道是否可以在其中添加

    欢迎所有建议和意见!我真的希望每一个细节都完美。

    Making a search result list (like in Google) is not very hard, if you just need something that works. Now, however, I want to do it with perfection, using the benefits of HTML5 semantics. The goal is to define the defacto way of marking up a search result list that potentially could be used by any future search engine.

    For each hit, I want to

    • order them by increasing number
    • display a clickable title
    • show a short summary
    • display additional data like categories, publishing date and file size

    My first idea is something like this:

    <ol>
      <li>
        <article>
          <header>
            <h1>
              <a href="url-to-the-page.html">
                The Title of the Page
              </a>
            </h1>
          </header>
          <p>A short summary of the page</p>
          <footer>
            <dl>
              <dt>Categories</dt>
              <dd>
                <nav>
                   <ul>
                      <li><a href="first-category.html">First category</a></li>
                      <li><a href="second-category.html">Second category</a></li>
                    </ul>
                </nav>
              </dd>
              <dt>File size</dt>
              <dd>2 kB</dd>
              <dt>Published</dt>
              <dd>
                <time datetime="2010-07-15T13:15:05-02:00" pubdate>Today</time>
              </dd>
            </dl>
          </footer>
        </article>
      </li>
      <li>
        ...
      </li>
      ...
    </ol>
    

    I am not really happy about the <article/> within the <li/>. First, the search result hit is not an article by itself, but just a very short summary of one. Second, I am not even sure you are allowed to put an article within a list.

    Maybe the <details/> and <summary/> tags are more suitable than <article/>, but I don't know if I can add a <footer/> inside that?

    All suggestions and opinions are welcome! I really want every single detail to be perfect.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

    发布评论

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

    评论(4

    叫思念不要吵 2024-09-17 04:40:26

    1) 我认为你应该坚持使用 article 元素,因为

    [t]他的article元素代表一个
    独立的组合物
    文档、页面、应用程序或站点
    其目的是
    可独立分发或
    可重复使用[来源]

    您只有一个单独文件的列表,所以我认为这是完全合适的。博客的首页也是如此,包含多个带有标题和大纲的帖子,每个帖子都位于一个单独的 article 元素中。此外,如果您打算引用文章中的几句话(而不是提供摘要),您甚至可以使用 blockquote 元素,例如 论坛帖子示例,显示用户正在回复的原始帖子。

    2) 如果您想知道是否允许在 li 元素中包含 article 元素,只需将其提供给验证器即可。正如您所看到的,这样做是被允许的。此外,正如工作草案< /a> 说:

    该元素可能存在的上下文
    使用:

    其中流内容< /a> 是预期的。


    3) 我不会对这些类别使用​​ nav 元素,因为这些链接不是页面主导航的一部分:

    只有由主要导航块组成的部分才适合nav 元素。特别是,页脚通常具有指向网站各个页面的简短链接列表,例如服务条款、主页和版权页面。 页脚 元素就足够了,不需要 nav 元素。 [来源]


    4) 不要使用 details 和/或 summary 元素,因为它们用作 交互式元素,不适用于普通文档。

    更新:关于使用(无)有序列表来呈现搜索结果是否是个好主意:

    ul 元素表示一个列表
    项目,其中项目的顺序是
    不重要——也就是说,在哪里
    改变顺序不会
    实质上改变了
    文档。 [来源]


    由于搜索结果列表实际上是一个列表,我认为这是合适的元素;但是,在我看来,顺序很重要(我希望最佳匹配结果位于列表的顶部),我认为您应该使用有序列表(ol< /代码>) 改为:

    ol 元素表示一个列表
    项目,项目所在的位置
    故意命令,使得
    改变顺序会改变
    该文件的含义。 [来源]


    使用 CSS,您可以简单地隐藏数字。

    编辑:哎呀,我刚刚意识到你已经使用了ol(由于我的疲劳,我以为你使用了ul)。我将保留我的“更新”不变;毕竟,它可能对某人有用。

    1) I think you should stick with the article element, as

    [t]he article element represents a
    self-contained composition in a
    document, page, application, or site
    and that is intended to be
    independently distributable or
    reusable [source]

    You merely have a list of separate documents, so I think this is fully appropriate. The same is true for the front page of a blog, containing several posts with titles and outlines, each in a separate article element. Besides, if you intend to quote a few sentences of the articles (instead of providing summaries), you could even use blockquote elements, like in the example of a forum post showing the original posts a user is replying to.

    2) If you're wondering if it's allowed to include article elements inside a li element, just feed it to the validator. As you can see, it is permitted to do so. Moreover, as the Working Draft says:

    Contexts in which this element may be
    used:

    Where flow content is expected.

    3) I wouldn't use nav elements for those categories, as those links are not part of the main navigation of the page:

    only sections that consist of major navigation blocks are appropriate for the nav element. In particular, it is common for footers to have a short list of links to various pages of a site, such as the terms of service, the home page, and a copyright page. The footer element alone is sufficient for such cases, without a nav element. [source]

    4) Do not use the details and/or summary elements, as those are used as part of interactive elements and are not intended for plain documents.

    UPDATE: Regarding if it's a good idea to use an (un)ordered list to present search results:

    The ul element represents a list of
    items, where the order of the items is
    not important — that is, where
    changing the order would not
    materially change the meaning of the
    document. [source]

    As a list of search results actually is a list, I think this is the appropriate element to use; however, as it seems to me that the order is important (I expect the best matching result to be on top of the list), I think that you should use an ordered list (ol) instead:

    The ol element represents a list of
    items, where the items have been
    intentionally ordered, such that
    changing the order would change the
    meaning of the document. [source]

    Using CSS you can simply hide the numbers.

    EDIT: Whoops, I just realized you already use an ol (due to my fatique, I thought you used an ul). I'll leave my ‘update’ as is; after all, it might be useful to someone.

    若无相欠,怎会相见 2024-09-17 04:40:26

    我会以这种方式标记它(不使用任何 RDFa/微数据词汇表或微格式;因此仅使用纯 HTML5 规范给出的内容):

    <ol start="1">
    
      <li id="1">
        <article>
         <h1><a href="url-to-the-page.html" rel="external">The Title of the Page</a></h1>
         <p>A short summary of the page</p>
         <footer>
           <dl>
             <dt>Categories</dt>
             <dd><a href="first-category.html">First category</a></dd>
             <dd><a href="second-category.html">Second category</a></dd>
             <dt>File size</dt>
             <dd>2 <abbr title="kilobyte">kB</code></dd>
             <dt>Published</dt>
             <dd><time datetime="2010-07-15T13:15:05-02:00">Today</time></dd>
            </dl>
          </footer>
        </article>
      </li>
    
      <li id="2">
        <article>
         …
        </article>
      </li>
    
    </ol>
    

    start attribute for ol

    如果搜索引擎使用分页,您应该给ol赋予start属性,以便每个li反映正确的排名位置。

    每个 liid

    每个 li 都应该获得 id 属性,以便您可以链接到它。该值应该是等级/位置。

    人们可能认为应该将 id 赋予 article,但我认为这是错误的:排名/顺序可能会随着时间而改变。您指的不是具体结果,而是结果位置。

    删除 header

    如果它仅包含标题 (h1),则不需要。

    rel="external" 添加到链接

    每个搜索结果的链接都是一个外部链接(通向不同的网站),因此应该获取 rel 外部

    删除nav

    类别链接不是文章范围内的导航。因此删除nav

    dd 中的每个类别

    您使用的

    <dt>Categories</dt>
    <dd>
     <ul>
      <li><a href="first-category.html">First category</a></li>
      <li><a href="second-category.html">Second category</a></li>
     </ul>
    </dd>
    

    :相反,您应该在其自己的 dd 中列出每个类别并删除 ul

    <dt>Categories</dt>
    <dd><a href="first-category.html">First category</a></dd>
    <dd><a href="second-category.html">Second category</a></dd>
    

    abbr< /code> 文件大小

    “2 kB”中的单位应该用 abbr 标记:

    2 <abbr title="kilobyte">kB</code>
    

    删除 pubdate 属性

    它不再出现在规范中。

    其他可以做的事情,

    • 如果链接结果的语言与搜索引擎的语言不同,则为链接提供 hreflang 属性,
    • 为链接描述和摘要提供 lang 属性(如果它是)使用与搜索引擎摘要不同的语言
    • :如果搜索引擎不创建,请使用 blockquote(带有 cite 属性)而不是 p摘要本身,但使用元描述或页面中的片段。
    • 标题/链接描述:如果链接描述正是链接网页的标题,则使用 q (带有 cite 属性)

    I'd markup it up this way (without using any RDFa/microdata vocabularies or microformats; so only using what the plain HTML5 spec gives):

    <ol start="1">
    
      <li id="1">
        <article>
         <h1><a href="url-to-the-page.html" rel="external">The Title of the Page</a></h1>
         <p>A short summary of the page</p>
         <footer>
           <dl>
             <dt>Categories</dt>
             <dd><a href="first-category.html">First category</a></dd>
             <dd><a href="second-category.html">Second category</a></dd>
             <dt>File size</dt>
             <dd>2 <abbr title="kilobyte">kB</code></dd>
             <dt>Published</dt>
             <dd><time datetime="2010-07-15T13:15:05-02:00">Today</time></dd>
            </dl>
          </footer>
        </article>
      </li>
    
      <li id="2">
        <article>
         …
        </article>
      </li>
    
    </ol>
    

    start attribute for ol

    If the search engine uses pagination, you should give the start attribute to the ol, so that each li reflects the correct ranking position.

    id for each li

    Each li should get id atribute, so that you can link to it. The value should be the rank/position.

    One could think that the id should be given to the article instead, but I think this would be wrong: the rank/order could change by time. You are not referring to a specific result but to a result position.

    Remove the header

    It is not needed if it contains only the heading (h1).

    Add rel="external" to the link

    The link to each search result is an external link (leading to a different website), so it should get the rel value external.

    Remove nav

    The category links are not navigation in scope of the article. So remove the nav.

    Each category in a dd

    You used:

    <dt>Categories</dt>
    <dd>
     <ul>
      <li><a href="first-category.html">First category</a></li>
      <li><a href="second-category.html">Second category</a></li>
     </ul>
    </dd>
    

    Instead, you should list each category in its own dd and remove the ul:

    <dt>Categories</dt>
    <dd><a href="first-category.html">First category</a></dd>
    <dd><a href="second-category.html">Second category</a></dd>
    

    abbr for file size

    The unit in "2 kB" should be marked-up with abbr:

    2 <abbr title="kilobyte">kB</code>
    

    Remove pubdate attribute

    It's not in the spec anymore.

    Other things that could be done

    • give hreflang attribute to the link if the linked result has a different language than the search engine
    • give lang attribute to the link description and the summary if it is in a different language than the search engine
    • summary: use blockquote (with cite attribute) instead of p, if the search engine does not create a summary itself but uses the meta-description or a snippet from the page.
    • title/link description: use q (with cite attribute) if the link description is exactly the title from the linked webpage
    云柯 2024-09-17 04:40:26

    追求“完美”的 HTML5 模板是徒劳的,因为规范本身远非完美,新“语义”元素的大多数规定用例充其量也很模糊。只要您的文档以逻辑方式构建,搜索引擎就不会有任何问题(大多数新标签不会产生丝毫影响)。事实上,严格遵循 HTML5 规范 - 例如,在每个新的分区元素中使用

    标签 - 可能会降低您的网站的可访问性(对于屏幕阅读器、例如)。不要追求“完美”或接近,因为它不存在 - HTML5 对此还没有经过深思熟虑。只需集中精力保持标记的逻辑性和整洁性即可。

    Aiming for a 'perfect' HTML5 template is futile because the spec itself is far from perfect, with most of the prescribed use-cases for the new 'semantic' elements obscure at best. As long as your document is structured in a logical fashion, you won't have any problems with search engines (most of the new tags don't have the slightest impact). Indeed, following the HTML5 spec to the letter - for example, using <h1> tags within each new sectioning element - may make your site less accessible (to screen readers, for example). Don't strive for 'perfect' or close-to, because it doesn't exist - HTML5 is not thought-out well enough for that. Just concentrate on keeping your markup logical and uncluttered.

    —━☆沉默づ 2024-09-17 04:40:26

    我发现 HTML5 的一个很好的资源是 HTML5Doctor。检查文章档案以了解新标签的实际实现。请注意,这不是一个完整的参考,但足以轻松入门:)

    页脚所示元素页面,部分可以包含页脚:)

    I found a good resource for HTML5 is HTML5Doctor. Check the article archive for practical implementations of the new tags. Not a complete reference mind you, but nice enough to ease into it :)

    As shown by the Footer element page, sections can contain footers :)

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