大网数据广告活动
网站运营与故障问答

Does itemprop have a different meaning on links vs. span tags?

· 52CE 我爱测

I'm using Schema.org markup for a Person , but I get inconsistent results in Google's Rich Snippet Testing Tool when using an <a> tag vs. a <span> tag. Take these two examples: <span itemprop="author" itemscope itemtype="http://s…

问题原文

原帖链接 · DisgruntledGoat · CC BY-SA 3.0

I'm using Schema.org markup for a Person, but I get inconsistent results in Google's Rich Snippet Testing Tool when using an <a> tag vs. a <span> tag. Take these two examples:

<span itemprop="author" itemscope itemtype="http://schema.org/Person">
  <a href="http://example.com/user/username">
    <span itemprop="name">username</span>
  </a>
</span>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
  <a href="http://example.com/user/username" itemprop="name">username</a>
</span>

The first one shows the name is parsed as username, but when I tried the second one to save on HTML, it shows the username is http://example.com/user/username - i.e. the URL of the link.

Why does this happen, and is it correct behaviour? I can't find any resources that make a distinction between itemprop on a link vs. a span.

已采纳解答

原帖链接 · unor · CC BY-SA 3.0

What the spec says

Yes, that behaviour is correct. Section 5.4 of the W3C Microdata spec describes which value gets used.

In most cases, the element’s content gets used as itemprop value, but in some cases, an attribute’s value gets used as itemprop value.

This is the case for these six attributes¹: content², src, href, data, value, datetime.

What this means in practice

If you want to use the content of an a element (instead of the URL in its href attribute) as property value, you have to use/add a suitable parent or child element:

<a href="ignored-url"><span itemprop="property">property value</span></a>
<span itemprop="property"><a href="ignored-url">property value</a></span>

If you want to provide a URL as property value, you must use itemprop on one of the URL property elements (i.e., elements that can³ have a href, src, or data attribute). So this would be invalid (unless you want to provide a string as value which just looks like a URL):

<!-- INVALID --> <span itemprop="property">http://example.com/foo</span>

¹ It must be valid for the element to have this attribute. So for an (invalid!) <div href="" itemprop=""></div>, the element content gets used, not the href value.

² In WHATWG’s and W3C’s old spec of Microdata, only the meta element can have the content attribute, but the new W3C Microdata spec (currently a Working Draft) allows content on any element. If an element has a content attribute and also one of the other listed attributes, content gets used.

³ Microdata makes it invalid to have one of the URL property elements without their respective URL attribute (i.e., href, src, data), except for the link element (but it’s already required by HTML to have the href attribute).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。