Element: getAttribute() メソッド
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.
getAttribute()
は Element
インターフェイスのメソッドで、この要素の指定された属性の値を返します。
指定された属性が存在しない場合、返される値は null
になります。
Attr
ノードのプロパティを調べる場合は、代わりに getAttributeNode()
メソッドを使用してください。
構文
js
getAttribute(attributeName)
引数
attributeName
-
値を取得したい属性の名前です。
返値
attributeName
の属性が存在すれば、値の入った文字列です。存在しなければ null
です。
例
html
<!-- HTML 文書内の div の例 -->
<div id="div1">Hi Champ!</div>
js
// コンソールへの出力
const div1 = document.getElementById("div1");
//=> <div id="div1">Hi Champ!</div>
const exampleAttr = div1.getAttribute("id");
//=> "div1"
const align = div1.getAttribute("align");
//=> null
解説
小文字化
HTML 文書とされている DOM の HTML 要素に対して呼び出すと、 getAttribute()
は処理前に引数を小文字化します。
ノンス値の受け取り
仕様書
Specification |
---|
DOM # ref-for-dom-element-getattribute① |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
getAttribute |
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.