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 GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setAttributeNode

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報