aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/id
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/element/id
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/element/id')
-rw-r--r--files/zh-cn/web/api/element/id/index.html73
1 files changed, 73 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/id/index.html b/files/zh-cn/web/api/element/id/index.html
new file mode 100644
index 0000000000..7bebb2d2eb
--- /dev/null
+++ b/files/zh-cn/web/api/element/id/index.html
@@ -0,0 +1,73 @@
+---
+title: Element.id
+slug: Web/API/Element/id
+tags:
+ - API
+ - DOM
+ - 元素
+ - 参考
+ - 属性
+translation_of: Web/API/Element/id
+---
+<div>{{ ApiRef("DOM") }}</div>
+
+<p>{{domxref("Element")}} 接口的 <strong><code>id</code></strong> 属性表示元素的标识符,与全局属性 <code><strong><a href="/en-US/docs/Web/HTML/Global_attributes/id">id</a></strong></code> 对应。</p>
+
+<p>同一文档中,若 <code>id</code> 的值不是空字符串 <code>""</code>,便必须是独特的;也就是说,不同元素的 ID 必须是不同的。</p>
+
+<p>这有助于让常用的 {{domxref("document.getElementById", "getElementById")}} 方法通过 <code>id</code> 的值找到对应的单个元素。另外,在 <a href="/zh-CN/docs/Web/CSS">CSS</a> 中,ID 可作为选择器使用,详见:<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors">ID 选择器</a>。</p>
+
+<div class="note">
+<p><strong>注意:</strong>虽然 ID 是区分大小写的, 但是不应该同时使用仅大小写有不同的 ID  值。</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">var <em>idStr</em> = <em>element</em>.id; // Get the id
+<em>element</em>.id = <em>idStr</em>; // Set the id
+</pre>
+
+<ul>
+ <li><code>idStr</code>,元素的 ID 属性值。</li>
+</ul>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('DOM WHATWG', '#dom-element-id', 'id')}}</td>
+ <td>{{Spec2('DOM WHATWG')}}</td>
+ <td>No change from {{SpecName('DOM2 HTML')}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM2 HTML', 'html.html#ID-63534901', 'id')}}</td>
+ <td>{{Spec2('DOM2 HTML')}}</td>
+ <td>No change from {{SpecName('DOM1')}}.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('DOM1', 'level-one-html.html#ID-63534901', 'id')}}</td>
+ <td>{{Spec2('DOM1')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Element.id")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>DOM 全局属性 <a href="/zh-CN/docs/Web/HTML/Global_attributes/id"><strong>id</strong></a>。</li>
+</ul>