Document: createAttribute() メソッド

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.

Document.createAttribute() は、新しい属性ノードを生成して返します。生成されたオブジェクトは Attr を実装したノードです。この方法では DOM は、特定の要素にどんな種類の属性を追加できるかを強制しません。

メモ: 引数に渡される文字列は小文字に変換されます。

構文

js
createAttribute(name)

引数

  • name は属性の名前を含む文字列です。

返値

Attr ノードです。

例外

InvalidCharacterError DOMException

name が無効な XML name である場合、例えば、数字・ハイフン・ピリオドで始まっている場合や、英数字・アンダースコア・ハイフン・ピリオド以外の文字を含む場合)に発生します。

js
const node = document.getElementById("div1");
const a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"

仕様書

Specification
DOM
# dom-document-createattribute

ブラウザーの互換性

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
createAttribute

Legend

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

Full support
Full support
Has more compatibility info.

関連情報