Element: setAttributeNode() メソッド
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
setAttributeNode()
メソッドは、新しい Attr
ノードを指定された要素に追加します。
構文
js
setAttributeNode(attribute)
引数
attribute
は要素に追加するAttr
ノードです。
返値
置換された属性ノードが、もしあれば、この関数から返されます。
例
この例では align
属性をある要素から別の要素へコピーしています。
HTML
html
<div id="one" align="left">one</div>
<div id="two">two</div>
JavaScript
js
let d1 = document.getElementById("one");
let d2 = document.getElementById("two");
let a = d1.getAttributeNode("align");
d2.setAttributeNode(a.cloneNode(true));
// 返値: 'left'
alert(d2.attributes[1].value);
メモ
指定された属性が要素に既に存在する場合、その属性は新しい属性に置き換えられ、置き換えられた属性が返されます。
このメソッドが使われることはあまりなく、通常は Element.setAttribute()
が要素の属性を変更するために使われます。
仕様書
Specification |
---|
DOM # dom-element-setattributenode |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
setAttributeNode |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.