【问题标题】:How do HTML link relations relate to fragment identifiers?HTML 链接关系如何与片段标识符相关联?
【发布时间】:2019-05-14 08:15:26
【问题描述】:

有时,我们使用rel 属性来传达指向特定页面或页面片段的链接的含义,如下所示:

<link rel="author" href="https://example.com/humans.txt">
<a rel="bookmark" href="https://example.com/page#thing">thing</a>

到目前为止,一切都很好。

案例

现在假设我有一个词汇表页面https://example.com/glossary,在那个页面上,我有一个包含一堆术语的定义列表:

<dl id="dictionary">
  <dt id="foo">foo</dt>
  <dd>A foo is not a <a href="#bar">bar</a>.</dd>
  <dt id="bar">bar</dt>
  <dd>A metric unit of pressure that serves alcoholic beverages.</dd>
</dl>

这让我们想知道链接到词汇表以及所述词汇表中的特定术语的正确/最佳方式是什么。我想出了以下选项:

词汇表:

1. <link rel="glossary" href="https://example.com/glossary">
2. <link rel="glossary" href="https://example.com/glossary#dictionary">

对于特定术语:

1. <a rel="bookmark" href="https://example.com/glossary#bar">bar</a>
2. <a rel="glossary" href="https://example.com/glossary#bar">bar</a>
3. <a rel="bookmark glossary" href="https://example.com/glossary#bar">bar</a>
4. <a href="https://example.com/glossary#bar">bar</a>

您认为最好的选择是什么,为什么?请注意,最好的方法完全有可能是我没有考虑过的。

【问题讨论】:

    标签: html hyperlink semantic-markup rel


    【解决方案1】:

    glossary 链接类型是指“提供与当前文档相关的术语表的文档”。

    bookmark 链接类型为最近的祖先article 提供永久链接,如果没有,则为最近的祖先部分

    因此,当您链接到出现在博客文章(或类似内容)中的术语的术语定义时,您不得使用 bookmark 类型,否则此博客文章的永久链接将是该术语的 URL定义。但由于博客文章包含词汇表中定义的术语,您可以使用 glossary 链接类型链接到词汇表。

    <!DOCTYPE html>
    <title>Blog post that contains a term defined in the glossary</title>
    <link rel="glossary" href="/glossary" />
    
    <article>
      <p>The <a href="/glossary#foo">foo</a> is great.</p>
    </article>
    

    glossary 提供片段标识符应该不会有什么坏处,甚至在少数情况下需要这样做:例如,当需要片段标识符来显示词汇表时,或者当文档还包含其他主要内容时到词汇表。


    注意:您可以在dt 元素中使用dfn 元素,并将id 属性移动到dfn 元素。

    <dl id="dictionary">
      <dt><dfn id="foo">foo</dfn></dt>
      <dd>A foo is not a <a href="#bar">bar</a>.</dd>
    </dl>
    

    您可能还对添加结构化数据感兴趣,using Schema.org’s DefinedTerm and DefinedTermSet

    【讨论】:

    • 谢谢!你是对的,新的规范几乎提供了答案。 (我认为bookmark 关系比articlesection 元素更早,但现在它们已按原样定义,几乎没有解释空间。)我想我希望添加一些语义归因于指向特定术语的链接,因此我可以相应地设置它们的样式。另一种机制将不得不做(也许只是一个class)。最后,我认为dt 中的dfn 是多余的,但我肯定会应用微数据。再次感谢!
    • @ACJ:我想说dt 中的dfn 不是多余的,因为dl 不再是定义 列表,而是描述 列表(可以采用任何类型的名称-值对)。通过使用dfn,您可以传达它是一个术语及其定义。
    • 啊,对。有时我的思绪会跳回旧的定义。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多