代码之家  ›  专栏  ›  技术社区  ›  Yuval Adam

用于添加自定义注释数据的语义HTML元素

  •  4
  • Yuval Adam  · 技术社区  · 9 年前

    假设我有一个HTML段落,我对它感兴趣(即添加一些不应该显示的上下文,只为文本添加含义)

    <p>The ultimate measure of a man is not where he stands 
    in moments of <tag data="some custom annotation">comfort and convenience</tag>,
    but where he stands at times of challenge and controversy.</p>
    

    在这种情况下应该使用的正确语义标记是什么?

    3 回复  |  直到 9 年前
        1
  •  1
  •   marcanuy    9 年前

    对于这种情况,有一个特定的标签,它指定语义而不显示内容。这是通过 meta 标记为 itemprop content 属性,并在的“入门”部分中进行了详细说明 schema.org :

    3c中。缺少/隐含信息:将meta标记用于内容

    有时,一个网页上有有价值的信息可以标记 但是信息不能被标记,因为它的方式 出现在页面上。信息可以在图像中传达(例如 例如,用于表示5分之4的评分的图像)或Flash 对象(例如,视频剪辑的持续时间),也可以是 隐含但未在页面上明确说明(例如 价格的货币)。

    在这些情况下,使用meta标记和content属性 指定信息。考虑这个例子,图像显示用户 五星级中的四星级:

    <div itemscope itemtype="http://schema.org/Offer">
      <span itemprop="name">Blend-O-Matic</span>
      <span itemprop="price">$19.95</span>
      <img src="four-stars.jpg" />
      Based on 25 user ratings
    </div>
    

    下面是一个示例,其中标记了评级信息。

    <div itemscope itemtype="http://schema.org/Offer">
      <span itemprop="name">Blend-O-Matic</span>
      <span itemprop="price">$19.95</span>
      <div itemprop="reviews" itemscope itemtype="http://schema.org/AggregateRating">
        <img src="four-stars.jpg" />
        <meta itemprop="ratingValue" content="4" />
        <meta itemprop="bestRating" content="5" />
        Based on <span itemprop="ratingCount">25</span> user ratings
      </div>
    </div>
    

    这项技术应谨慎使用。对于无法标记的信息,只能将meta与内容一起使用。

        2
  •  1
  •   Community Mohan Dere    8 年前

    您不使用特定的HTML标记,而是使用语义注释。您有两个选项: Microdata RDFa .

    两者都有 similar capabilities 并且被搜索引擎爬虫理解。这意味着谷歌是Microdata的支持者。

    有关现成可用的示例,您可以参考 schema.org website 每个术语定义都显示了两种序列化的用法。您的HTML可以是:

    微数据

    <p itemscope itemtype="http://schema.org/Person">
      The ultimate measure of a man is not where he stands in moments of 
      <span itemprop="description">comfort and convenience</span>,
      but where he stands at times of challenge and controversy.
    </p>
    

    资源描述框架

    <p vocab="http://schema.org/" typeof="Person">
      The ultimate measure of a man is not where he stands in moments of 
      <span property="description">comfort and convenience</span>,
      but where he stands at times of challenge and controversy.
    </p>
    

    正如你所看到的,我只是使用 <span> 以封装带注释的文本。除此之外,RDFa和Microdata并没有什么不同。

    此外,请查看 RDF Translator app 。它将帮助您处理注释。

        3
  •  0
  •   VorganHaze    5 年前

    您可以考虑使用“自定义数据属性”。例如: <p data-custom-name="any-value">...</p> .

    请参见: https://html5doctor.com/html5-custom-data-attributes/ https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes