itemprop

itemprop 全局属性 用于向项目添加属性。每个 HTML 元素都可以指定一个 itemprop 属性,itemprop 由名称/值对组成。每个名称-值对称为属性,一组一个或多个属性形成一个项目。属性值可以是字符串或 URL,可以与多种元素关联,包括 <audio><embed><iframe><img><link><object><source><track><video>

¥The itemprop global attribute is used to add properties to an item. Every HTML element can have an itemprop attribute specified, and an itemprop consists of a name-value pair. Each name-value pair is called a property, and a group of one or more properties forms an item. Property values are either a string or a URL and can be associated with a very wide range of elements including <audio>, <embed>, <iframe>, <img>, <link>, <object>, <source>, <track>, and <video>.

示例

¥Examples

下面的示例显示了一组标有 itemprop 属性的元素的源,后面的表格显示了生成的结构化数据。

¥The example below shows the source for a set of elements marked up with itemprop attributes, followed by a table showing the resulting structured data.

HTML

html
<div itemscope itemtype="http://schema.org/Movie">
  <h1 itemprop="name">Avatar</h1>
  <span>
    Director:
    <span itemprop="director">James Cameron</span>
    (born August 16, 1954)
  </span>
  <span itemprop="genre">Science fiction</span>
  <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">
    Trailer
  </a>
</div>

结构化数据

¥Structured data

项目
项目名称 itemprop 值
itemprop name 阿凡达
itemprop director 詹姆斯·卡梅伦
itemprop genre 科幻小说
itemprop trailer ../movies/avatar-theatrical-trailer.html

属性

¥Properties

属性的值可以是字符串或 URL。当字符串值是 URL 时,它使用 <a> 元素及其 href 属性、<img> 元素及其 src 属性或链接到或嵌入外部资源的其他元素来表示。

¥Properties have values that are either a string or a URL. When a string value is a URL, it is expressed using the <a> element and its href attribute, the <img> element and its src attribute, or other elements that link to or embed external resources.

三个值为字符串的属性

¥Three properties with values that are strings

html
<div itemscope>
  <p>My name is <span itemprop="name">Neil</span>.</p>
  <p>My band is called <span itemprop="band">Four Parts Water</span>.</p>
  <p>I am <span itemprop="nationality">British</span>.</p>
</div>

一个属性 "image",其值为 URL

¥One property, "image", whose value is a URL

html
<div itemscope>
  <img itemprop="image" src="google-logo.png" alt="Google" />
</div>

当一个字符串值不易被人读取和理解时(例如,一长串数字和字母),可以使用数据元素的 value 属性来显示,其中更容易被人理解 元素内容中给出的人类版本(不是结构化数据的一部分) - 请参见下面的示例)。

¥When a string value can't be easily read and understood by a person (e.g., a long string of numbers and letters), it can be displayed using the value attribute of the data element, with the more easily-understood-by-a human-version given in the element's contents (which is not part of the structured data - see example below).

具有值为产品 ID 的属性的项目

¥An item with a property whose value is a product ID

该 ID 不人性化,因此使用产品名称代替。

¥The ID is not human-friendly, so the product's name is used instead.

html
<h1 itemscope>
  <data itemprop="product-id" value="9678AOU879">The Instigator 2000</data>
</h1>

对于数值数据,可以使用 meter 元素及其 value 属性。

¥For numeric data, the meter element and its value attribute can be used.

米元素

¥A meter element

html
<div itemscope itemtype="http://schema.org/Product">
  <span itemprop="name">Panasonic White 60L Refrigerator</span>
  <img src="panasonic-fridge-60l-white.jpg" alt="" />
  <div
    itemprop="aggregateRating"
    itemscope
    itemtype="http://schema.org/AggregateRating">
    <meter itemprop="ratingValue" min="0" value="3.5" max="5">
      Rated 3.5/5
    </meter>
    (based on <span itemprop="reviewCount">11</span>
    customer reviews)
  </div>
</div>

类似地,对于与日期和时间相关的数据,可以使用时间元素及其日期时间属性。

¥Similarly, for date- and time-related data, the time element and its datetime attribute can be used.

具有一个属性 "birthday" 的项目,其值为日期

¥An item with one property, "birthday", whose value is a date

html
<div itemscope>
  I was born on
  <time itemprop="birthday" datetime="1984-05-10">May 10th 1984</time>.
</div>

通过将 itemscope 属性放在声明属性的元素上,属性也可以是名称/值对组。每个值都是一个字符串或一组名称-值对(即一个项目)。

¥Properties can also be groups of name-value pairs, by putting the itemscope attribute on the element that declares the property. Each value is either a string or a group of name-value pairs (i.e. an item).

外部代表一个人,内部代表一个乐队

¥An outer item representing a person, and an inner one representing a band

html
<div itemscope>
  <p>Name: <span itemprop="name">Amanda</span></p>
  <p>
    Band:
    <span itemprop="band" itemscope>
      <span itemprop="name">Jazz Band</span>
      (<span itemprop="size">12</span> players)
    </span>
  </p>
</div>

上面的外部项目有两个属性,"name" 和 "band"。"name" 是 "阿曼达","band" 本身就是一个项目,具有两个属性:"name" 和 "size"。乐队的 "name" 是 "爵士乐队","size" 是 "12"。此示例中的外部项是顶层微数据项。不属于其他项目的项目称为顶层微数据项目。

¥The outer item above has two properties, "name" and "band". The "name" is "Amanda", and the "band" is an item in its own right, with two properties, "name" and "size". The "name" of the band is "Jazz Band", and the "size" is "12". The outer item in this example is a top-level microdata item. Items that are not part of others are called top-level microdata items.

所有属性与其物品分开

¥All the properties separated from their items

此示例与上一个示例相同,但所有属性都与其项目分开。

¥This example is the same as the previous one, but all the properties are separated from their items.

html
<div itemscope id="amanda" itemref="a b"></div>
<p id="a">Name: <span itemprop="name">Amanda</span></p>
<div id="b" itemprop="band" itemscope itemref="c"></div>
<div id="c">
  <p>Band: <span itemprop="name">Jazz Band</span></p>
  <p>Size: <span itemprop="size">12</span> players</p>
</div>

这给出了与前面的示例相同的结果。第一个项目有两个属性:"name"(设置为 "阿曼达")和 "band"(设置为另一个项目)。第二个项目还有两个属性:"name"(设置为 "爵士乐队")和 "size"(设置为 "12")。

¥This gives the same result as the previous example. The first item has two properties, "name", set to "Amanda", and "band", set to another item. That second item has two further properties, "name", set to "Jazz Band", and "size", set to "12".

一个项目可以具有多个具有相同名称和不同值的属性。

¥An item can have multiple properties with the same name and different values.

两种口味的冰淇淋

¥Ice cream with two flavors

html
<div itemscope>
  <p>Flavors in my favorite ice cream:</p>
  <ul>
    <li itemprop="flavor">Lemon sorbet</li>
    <li itemprop="flavor">Apricot sorbet</li>
  </ul>
</div>

这会产生一个具有两个属性的项目,名称均为 "flavor",值分别为 "柠檬冰糕" 和 "杏冰糕"。

¥This results in an item with two properties, both with the name "flavor" and having the values "Lemon sorbet" and "Apricot sorbet".

引入属性的元素还可以一次引入多个属性,以避免某些属性具有相同值时出现重复。

¥An element introducing a property can also introduce multiple properties at once, to avoid duplication when some of the properties have the same value.

具有两个属性 "favorite-color" 和 "favorite-fruit" 的项目,均设置为值 "orange"

¥An item with two properties, "favorite-color" and "favorite-fruit", both set to the value "orange"

html
<div itemscope>
  <span
    itemprop="favorite-color
    favorite-fruit"
    >orange
  </span>
</div>

注意:微数据与标记微数据的文档内容之间不存在任何关系。

¥Note: There is no relationship between the microdata and the content of the document where the microdata is marked up.

以两种不同方式标记的相同结构化数据

¥Same structured data marked up in two different ways

以下两个示例之间没有语义差异

¥There is no semantic difference between the following two examples

html
<figure>
  <img src="castle.jpeg" />
  <figcaption>
    <span itemscope><span itemprop="name">The Castle</span></span> (1986)
  </figcaption>
</figure>
html
<span itemscope><meta itemprop="name" content="The Castle" /></span>
<figure>
  <img src="castle.jpeg" />
  <figcaption>The Castle (1986)</figcaption>
</figure>

两者都有一个带有标题的图形,并且都与图形完全无关,都有一个带有名称 "name" 和值 "城堡" 的名称-值对的项目。唯一的区别是,如果用户将 Figcaption 拖出文档,该项目将包含在拖放数据中。与该项目关联的图片将不包括在内。

¥Both have a figure with a caption, and both, completely unrelated to the figure, have an item with a name-value pair with the name "name" and the value "The Castle". The only difference is that if the user drags the figcaption out of the document, the item will be included in the drag-and-drop data. The image associated with the item won't be included.

名称和值

¥Names and values

属性是一组无序的唯一标记,区分大小写并表示名称/值对。属性值必须至少有一个标记。在下面的示例中,每个数据单元格都是一个令牌。

¥A property is an unordered set of unique tokens that are case-sensitive and represent the name-value pairs. The property value must have at least one token. In the example below, each data cell is a token.

名称示例

¥Names examples

物品
itemprop 名称 itemprop
itemprop country 爱尔兰
itemprop 选项 2
itemprop [https://www.flickr.com/photos/nlireland/6992065114/](https://www.flickr.com/photos/nlireland/6992065114/) 凯里之环
itemprop img [https://www.flickr.com/photos/nlireland/6992065114/](https://www.flickr.com/photos/nlireland/6992065114/)
itemprop website flickr
itemprop (令牌) (令牌)

令牌可以是字符串或 URL。如果某个项目是 URL,则该项目称为类型化项目。否则,它是一个字符串。字符串不能包含句点或冒号(见下文)。

¥Tokens are either strings or URL's. An item is called a typed item if it is a URL. Otherwise, it is a string. Strings cannot contain a period or a colon (see below).

  1. 如果该项目是键入的项目,则它必须是:
    1. 定义的属性名称,或
    2. 有效的 URL,引用词汇表定义,或者
    3. 用作专有项目属性名称的有效 URL(即公共规范中未定义的 URL),或
  2. 如果该项目不是键入的项目,则它必须是:
    1. 不包含“.”(U+002E 句号)字符和“:”字符(U+003A COLON)的字符串,用作专有项目属性名称(同样,公共规范中未定义)。

注意:上述规则不允许非 URL 值中包含 ":" 字符,否则无法将它们与 URL 区分开。带有 "." 字符的值保留用于将来的扩展。不允许使用空格字符,因为否则这些值将被解析为多个标记。

¥Note: The rules above disallow ":" characters in non-URL values because otherwise they could not be distinguished from URLs. Values with "." characters are reserved for future extensions. Space characters are disallowed because otherwise the values would be parsed as multiple tokens.

¥Values

名称-值对的属性值由以下列表中第一个匹配案例给出:

¥The property value of a name-value pair is as given for the first matching case in the following list:

  • 如果元素具有 itemscope 属性
    • 该值是该元素创建的项目
  • 如果元素是 meta 元素
    • 该值是元素的 content 属性的值
  • 如果元素是 audioembediframeimgsourcetrackvideo 元素
    • 该值是结果 URL 字符串,该字符串是通过解析元素的 src 属性值相对于设置属性时元素的节点文档(微数据 DOM API 的一部分)得到的结果
  • 如果元素是 aarealink 元素
    • 该值是结果 URL 字符串,该字符串是通过解析元素的 href 属性值(相对于设置该属性时该元素的节点文档)得到的结果
  • 如果元素是 object 元素
    • 该值是结果 URL 字符串,该字符串是通过解析元素的数据属性值(相对于设置属性时元素的节点文档)而得到的
  • 如果元素是 data 元素
    • value 是元素的 value 属性的值
  • 如果元素是 meter 元素
    • 该值是元素的 value 属性的值
  • 如果元素是 time 元素
    • 该值为元素的 datetime

否则

¥Otherwise

  • 该值是元素的文本内容。

如果属性的值为 URL,则必须使用 URL 属性元素指定该属性。URL 属性元素是 aareaaudioembediframeimglinkobjectsourcetrackvideo 元素。

¥If a property's value is a URL, the property must be specified using a URL property element. The URL property elements are the a, area, audio, embed, iframe, img, link, object, source, track, and video elements.

名称顺序

¥Name order

名称相对于彼此是无序的,但如果特定名称具有多个值,则它们确实具有相对顺序。

¥Names are unordered relative to each other, but if a particular name has multiple values, they do have a relative order.

在以下示例中,"a" 属性按顺序具有值 "1" 和 "2",但 "a" 属性是否位于 "b" 属性之前并不重要。

¥In the following example, the "a" property has the values "1" and "2", in that order, but whether the "a" property comes before the "b" property or not is not important.

html
<div itemscope>
  <p itemprop="a">1</p>
  <p itemprop="a">2</p>
  <p itemprop="b">test</p>
</div>

下面是几个等效的例子:

¥Here are several equivalent examples:

html
<div itemscope>
  <p itemprop="b">test</p>
  <p itemprop="a">1</p>
  <p itemprop="a">2</p>
</div>
html
<div itemscope>
  <p itemprop="a">1</p>
  <p itemprop="b">test</p>
  <p itemprop="a">2</p>
</div>
html
<div id="x">
  <p itemprop="a">1</p>
</div>
<div itemscope itemref="x">
  <p itemprop="b">test</p>
  <p itemprop="a">2</p>
</div>

表示一本书的结构化数据

¥Representing structured data for a book

此示例使用微数据属性来表示以下结构化数据:

¥This example uses microdata attributes to represent the following structured data:

itemscope 物品种类:itemid [https://schema.org/Book](https://schema.org/Book):瓮:isbn:0-374-22848-5
itemprop title 东方冰之猫头鹰
itemprop author 乔纳森·C·斯拉特
itemprop datePublished 2020-08-04

HTML

html
<dl
  itemscope
  itemtype="https://schema.org/Book"
  itemid="urn:isbn:0-374-22848-5<">
  <dt>Title</dt>
  <dd itemprop="title">Owls of the Eastern Ice</dd>
  <dt>Author</dt>
  <dd itemprop="author">Jonathan C Slaght</dd>
  <dt>Publication date</dt>
  <dd>
    <time itemprop="datePublished" datetime="2020-08-04">August 4 2020</time>
  </dd>
</dl>

结果

¥Result

规范

Specification
HTML Standard
# names:-the-itemprop-attribute

¥Specifications

也可以看看

¥See also