構文
 
 要素.getAttribute(属性) [メソッド]
属性
 
 値を取得する属性
総合 インデックス
メソッド
FirefoxIE

説明

  •  指定した要素の設定した属性の値を取得すメソッド。
  •  用例では,id名として「IMG」を設定した,document要素のimg要素が持っているsrc属性,border属性,width属性など,色々な属性の値を取得している。
  •  Internet Explorerは,バージョン5以降,Mozilla系ブラウザではNavigator6.0以降で対応。

用例

id名「IMG」を設定したimg要素から,「src」属性,「border」属性,「width」属性,「height」属性,「alt」属性の値を取得し書き出す。
<p>
<img src="image1.jpg" id="IMG" border="4" width="100" height="100"
 alt="IMAGE_1">
</p>
<hr>
<script type="text/javascript">
<!--
document.write("src属性:")
document.write(document.getElementById("IMG").getAttribute("src"))
document.write("<br>")
document.write("border属性:")
document.write(document.getElementById("IMG").getAttribute("border"))
document.write("<br>")
document.write("width属性:")
document.write(document.getElementById("IMG").getAttribute("width"))
document.write("<br>")
document.write("height属性:")
document.write(document.getElementById("IMG").getAttribute("height"))
document.write("<br>")
document.write("alt属性:")
document.write(document.getElementById("IMG").getAttribute("alt"))
//-->
</script>

この用例を実行する