diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/element/getattributenode | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/element/getattributenode')
-rw-r--r-- | files/zh-cn/web/api/element/getattributenode/index.html | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/getattributenode/index.html b/files/zh-cn/web/api/element/getattributenode/index.html new file mode 100644 index 0000000000..47d7e5c206 --- /dev/null +++ b/files/zh-cn/web/api/element/getattributenode/index.html @@ -0,0 +1,48 @@ +--- +title: Element.getAttributeNode() +slug: Web/API/Element/getAttributeNode +tags: + - API + - DOM +translation_of: Web/API/Element/getAttributeNode +--- +<p>{{ APIRef("DOM") }}</p> + +<h2 id="概要">概要</h2> + +<p>返回指定元素的指定属性节点</p> + +<h2 id="语法">语法</h2> + +<pre class="eval"><em>var attrNode</em> = <em>element</em>.getAttributeNode(<em>attrName</em>); +</pre> + +<ul> + <li><code>attrNode</code> 获得的属性返回值,是Attr 节点, nodeType 为 2</li> + <li><code>attrName</code> 是一个包含属性名称的 字符串</li> +</ul> + +<h2 id="举例">举例</h2> + +<pre>// html: <div id="top" /> +var t = document.getElementById("top"); +var idAttr = t.getAttributeNode("id"); +alert(idAttr.value == "top") +</pre> + +<h2 id="注意">注意</h2> + +<p>当在一个被标记为HTML文档的DOM中的HTML元素上调用这个方法时, getAttributeNode会将参数转变为小写形式。</p> + +<p>Attr 节点继承自Node,但不被认为是文档树的一部分。Node上定义的常用属性,如 parentNode, previousSibling, 和 nextSibling 对于 Attr节点来说都为null。然而,你可以使用 ownerElement 来得到拥有这个属性的元素。</p> + +<p><a href="/en/DOM/element.getAttribute" title="en/DOM/element.getAttribute">getAttribute</a> 通常用于替换getAttributeNode方法,来获得元素的属性值。性能也更快. 性能对比是 element.id 大于 element.getAttribute('id') 大于 element.getAttributeNode('id').nodeValue.</p> + +<p>{{ DOMAttributeMethods() }}</p> + +<h2 id="规范">规范</h2> + +<ul> + <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-217A91B8">DOM Level 2 Core: getAttributeNode</a> (introduced in <a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-getAttributeNode">DOM Level 1 Core</a>)</li> + <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#apis-in-html-documents">HTML 5: APIs in HTML documents</a></li> +</ul> |