HTML DOM要素getAttribute()
❮要素オブジェクト例
要素のクラス属性の値を取得します。
let value = document.getElementById("myId").getAttribute("class");
以下のその他の例。
定義と使用法
この getAttribute()
メソッドは、要素の属性の値を返します。
チュートリアル
関連項目:
構文
element.getAttribute(attributename)
パラメータ値
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute you want to get the value from |
技術的な詳細
戻り値: | 指定された属性の値を表す文字列。 注:属性が存在しない場合、戻り値はnullまたは空の文字列( "")です。 |
---|---|
DOMバージョン | コアレベル1要素オブジェクト |
その他の例
例
<a>要素のターゲット属性の値を取得します。
var x = document.getElementById("myAnchor").getAttribute("target");
例
<button>要素のonclickイベント属性の値を取得します。
var x = document.getElementById("myBtn").getAttribute("onclick");
ブラウザのサポート
element.getAttribute
DOMレベル1(1998)の機能です。
すべてのブラウザで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
❮要素オブジェクト