itemscope - HTML: HyperText Markup Language 编辑

itemscope is a boolean global attribute that defines the scope of associated metadata. Specifying the itemscope attribute for an element creates a new item, which results in a number of name-value pairs that are associated with the element. A related attribute, itemtype, is used to specify the valid URL of a vocabulary (such as schema.org) that describes the item and its properties context. In each of the following examples, the vocabulary is from schema.org.

Every HTML element may have an itemscope attribute specified. An itemscope element that does not have an associated itemtype must have an associated itemref.

Note: Find more about itemtype attributes at http://schema.org/Thing

Simple example

HTML

The following example specifies the itemscope attribute. The example specifies the itemtype as "http://schema.org/Movie", and specifies three related itemprop attributes.

<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="https://youtu.be/0AY1XIkX7bY" itemprop="trailer">Trailer</a>
</div>

Structured data

The following table shows the structured data from the preceding example.

itemscopeItemtypeMovie
itemprop(itemprop name)(itemprop value)
itempropdirectorJames Cameron
itempropgenreScience Fiction
itempropnameAvatar
itemprophttps://youtu.be/0AY1XIkX7bYTrailer

itemscope id attributes

When you specify the itemscope attribute for an element, a new item is created. The item consists of a group of name-value pairs. For elements with an itemscope attribute and an itemtype attribute, you may also specify an id attribute. You can use the id attribute to set a global identifier for the new item. A global identifier allows the item to relate to other items found on pages across the Web.

Example

There are four itemscope attributes in the following example. Each itemscope attribute sets the scope of its corresponding itemtype attribute. The itemtypes, Recipe, AggregateRating, and NutritionInformation in the following example are part of the schema.org structured data for a recipe, as specified by the first itemtype, http://schema.org/Recipe.

<div itemscope itemtype="http://schema.org/Recipe">
  <h2 itemprop="name">Grandma's Holiday Apple Pie</h2>
  <img itemprop="image" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/42759561_8631e2f905_n.jpg" width="50" height="50" />
  <p>
    By <span itemprop="author" itemscope itemtype="http://schema.org/Person">
      <span itemprop="name">Carol Smith</span>
    </span>
  </p>
  <p>
    Published: <time datetime="2009-11-05" itemprop="datePublished">November 5, 2009</time>
  </p>
  <span itemprop="description">This is my grandmother's apple pie recipe. I like to add a dash of nutmeg.</span>
  <br>
  <span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
    <span itemprop="ratingValue">4.0</span> stars based on <span itemprop="reviewCount">35</span> reviews
  </span>
  <br>
  Prep time: <time datetime="PT30M" itemprop="prepTime">30 min</time><br>
  Cook time: <time datetime="PT1H" itemprop="cookTime">1 hou</time>r<br>
  Total time: <time datetime="PT1H30M" itemprop="totalTime">1 hour 30 min</time><br>
  Yield: <span itemprop="recipeYield">1 9" pie (8 servings)</span><br>
  <span itemprop="nutrition" itemscope itemtype="http://schema.org/NutritionInformation">
    Serving size: <span itemprop="servingSize">1 medium slice</span><br>
    Calories per serving: <span itemprop="calories">250 cal</span><br>
    Fat per serving: <span itemprop="fatContent">12 g</span><br>
  </span>
  <p>
    Ingredients:<br>
    <span itemprop="recipeIngredient">Thinly-sliced apples: 6 cups<br></span>
    <span itemprop="recipeIngredient">White sugar: 3/4 cup<br></span>
    ...
  </p>
  Directions: <br>
  <div itemprop="recipeInstructions">
    1. Cut and peel apples<br>
    2. Mix sugar and cinnamon. Use additional sugar for tart apples. <br>
    ...
  </div>
</div>

Results

HTML

The following is an example rendering of the preceding code example.

Structured data

itemscopeitemtypeRecipe
itempropnameGrandma's Holiday Apple Pie
itempropimagehttps://www.wenjiangs.com/wp-content/uploads/2020/mozilla/42759561_8631e2f905_n.jpg
itempropdatePublished2009-11-05
itempropdescriptionThis is my grandmother's apple pie recipe. I like to add a dash of nutmeg.
itempropprepTimePT30M
itempropcookTimePT1H
itemproptotalTimePT1H30M
itemproprecipeYield1 9" pie (8 servings)
itemproprecipeIngredientThinly-sliced apples: 6 cups
itemproprecipeIngredientWhite sugar: 3/4 cup
itemproprecipeInstructions1. Cut and peel apples 2. Mix sugar and cinnamon. Use additional sugar for tart apples .
itempropauthor [Person]
itempropnameCarol Smith
itemscopeitemprop[itemtype]aggregateRating [AggregateRating]
itempropratingValue4.0
itempropreviewCount35
itemscopeitemprop[itemtype]nutrition [NutritionInformation]
itempropservingSize1 medium slice
itempropcalories250 cal
itempropfatContent12 g

Note: A handy tool for extracting microdata structures from HTML is Google's Structured Data Testing Tool. Try it on the HTML shown above.

Specifications

SpecificationStatusComment
HTML Microdata
The definition of 'itemscope' in that specification.
Working Draft
HTML Living Standard
The definition of 'itemscope' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:96 次

字数:11265

最后编辑:8年前

编辑次数:0 次

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