aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/html/global_attributes/id/index.html
blob: d1ccbcd3ecf6341e78577dae1ae8d770f25044ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: id
slug: Web/HTML/Global_attributes/id
translation_of: Web/HTML/Global_attributes/id
---
<div>{{HTMLSidebar("Global_attributes")}}</div>

<p><span class="seoSummary"><strong><code>id</code> <a href="/zh-CN/docs/Web/HTML/Global_attributes">全局属性</a></strong>定义了一个全文档唯一的标识符 (ID)。它用于在链接(使用<a href="/zh-CN/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#片段">片段</a>)、脚本和样式(通过 {{glossary("CSS")}})中辨识元素。</span></p>

<div>{{EmbedInteractiveExample("pages/tabbed/attribute-id.html","tabbed-shorter")}}</div>

<div class="blockIndicator warning">
<p>该属性的值是一个不透明(opaque)字符串,这意味着网页开发者不能使用它来传递人类可读的信息。</p>
</div>

<p><code>id</code> 的值不得包含空白字符({{glossary("whitespace")}},包括空格和制表符等)。浏览器会将不符合规范的 ID 中的空白字符视为 ID 的一部分。与允许以空格分隔值的 {{htmlattrxref("class")}} 属性不同,元素只能拥有一个 ID 值。</p>

<div class="note">
<p><strong>注意:</strong>使用除 {{glossary("ASCII")}} 字母、数字、<code>_</code><code>-</code><code>.</code> 以外的字符可能会造成兼容性问题,因为 HTML 4 中不允许使用它们。虽然这个限制在 {{glossary("HTML5")}} 中被解除了,但为兼容性考虑 ID 应该以字母开头。</p>
</div>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "dom.html#the-id-attribute", "id")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No change from latest snapshot, {{SpecName('HTML5.1')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5.1', "dom.html#the-id-attribute", "id")}}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td>Snapshot of {{SpecName('HTML WHATWG')}}, no change from {{SpecName('HTML5 W3C')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', "dom.html#the-id-attribute", "id")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td>Snapshot of {{SpecName('HTML WHATWG')}}, now accept <code>'_'</code>, <code>'-'</code> and <code>'.'</code> if not at the beginning fo the id. It is also a true global attribute.</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML4.01', 'struct/global.html#adef-id', 'id')}}</td>
   <td>{{Spec2('HTML4.01')}}</td>
   <td>Supported on all elements but {{HTMLElement("base")}}, {{HTMLElement("head")}}, {{HTMLElement("html")}}, {{HTMLElement("meta")}}, {{HTMLElement("script")}}, {{HTMLElement("style")}}, and {{HTMLElement("title")}}.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("html.global_attributes.id")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li>所有的<a href="/zh-CN/docs/Web/HTML/Global_attributes">全局属性</a></li>
 <li>反映该属性的 {{domxref("Element.id")}}</li>
</ul>