aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/setattributenode
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/setattributenode
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/setattributenode')
-rw-r--r--files/zh-cn/web/api/element/setattributenode/index.html46
1 files changed, 46 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/setattributenode/index.html b/files/zh-cn/web/api/element/setattributenode/index.html
new file mode 100644
index 0000000000..1d9c641c8e
--- /dev/null
+++ b/files/zh-cn/web/api/element/setattributenode/index.html
@@ -0,0 +1,46 @@
+---
+title: Element.setAttributeNode()
+slug: Web/API/Element/setAttributeNode
+tags:
+ - API
+ - DOM
+ - Element
+translation_of: Web/API/Element/setAttributeNode
+---
+<p>{{ APIRef("DOM") }}</p>
+
+<p><code>setAttributeNode()</code> 为指定的 Element 添加属性节点.</p>
+
+<h2 id="Syntax" name="Syntax">Syntax</h2>
+
+<pre class="eval"><em>var replacedAttr</em> = <em>element</em>.setAttributeNode(<em>attribute</em>);
+</pre>
+
+<ul>
+ <li><code>attribute是添加到 element 中的属性节点</code>.</li>
+ <li><code>replacedAttr</code> 是被替换掉的属性节点。 如果存在原有属性, 则函数返回原有属性节点. </li>
+</ul>
+
+<h2 id="Example" name="Example">Example</h2>
+
+<pre>// &lt;div id="one" align="left"&gt;one&lt;/div&gt;
+// &lt;div id="two"&gt;two&lt;/div&gt;
+var d1 = document.getElementById("one");
+var d2 = document.getElementById("two");
+var a = d1.getAttributeNode("align");
+d2.setAttributeNode(a.cloneNode(true));
+alert(d2.attributes[1].value)
+// returns: `left'
+</pre>
+
+<h2 id="Notes" name="Notes">Notes</h2>
+
+<p>如果 element 中已经存在该属性名的属性,则函数使用新的属性替换掉原有的属性并将原有属性返回</p>
+
+<p>这个方法很少被用到, 多数情况下使用函数 <code><a href="/en/DOM/element.setAttribute" title="en/DOM/element.setAttribute">setAttribute()</a></code> 修改 element 的属性.</p>
+
+<p>{{ DOMAttributeMethods() }}</p>
+
+<h2 id="Specification" name="Specification">Specification</h2>
+
+<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-887236154">DOM Level 2 Core: setAttributeNode</a> (introduced in <a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-setAttributeNode">DOM Level 1 Core</a>)</p>