Element: removeAttribute() メソッド

ElementremoveAttribute() メソッドは、指定された名前の属性を要素から削除します。

構文

js
removeAttribute(attrName)

引数

attrName

文字列で、要素から削除する属性名を指定します。指定した属性が存在しなかった場合、removeAttribute() はエラーを発生させずに戻ります。

返値

なし (undefined)。

使用上のメモ

属性の値を直接、または setAttribute() を使用して null に設定するのではなく、 removeAttribute() を使用してください。多くの属性は null に設定しても、期待通りの動作をしません。

DOM メソッドは要素の属性を取り扱います。

名前空間に無関係、
最も一般的に使用されるメソッド
名前空間に限定される変数
(DOM Level 2)
Attr ノードを直接扱う DOM レベル 1 のメソッド
(ほとんど使用されない)
Attr ノードを直接扱う DOM レベル 2 名前空間に限定されるメソッド
(ほとんど使用されない)
setAttribute (DOM 1) setAttributeNS setAttributeNode setAttributeNodeNS
getAttribute (DOM 1) getAttributeNS getAttributeNode getAttributeNodeNS
hasAttribute (DOM 2) hasAttributeNS - -
removeAttribute (DOM 1) removeAttributeNS removeAttributeNode -

js
// Given: <div id="div1" align="left" width="200px">
document.getElementById("div1").removeAttribute("align");
// Now: <div id="div1" width="200px">

仕様書

Specification
DOM Standard
# ref-for-dom-element-removeattribute①

ブラウザーの互換性

BCD tables only load in the browser