DOMによる属性の設定
DOMにより要素ノードの属性の値を設定する場合、setAttribute()メソッドを使用します。
setAttribute()メソッド
要素ノード.setAttribute(属性名,属性値)
以下のサンプルではsetAttribute()メソッドによりイメージファイルのファイル名を変更しています。
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function setAttributes(){
var img1 = document.getElementById("img1");
img1.setAttribute("src","bkimgArea.jpg");
}
</script>
<body>
<img id="img1" src="imgFile1.jpg" name="file1" alt="イメージファイル1"
width="50px" height="50px" lowsrc="imglowFile1.jpg"
border="2" hspace="10" vspace="15">
<form name="fm">
<input type="button" value="変更" onclick="setAttributes()">
</form>
</body>
</html>
DOMとは | ノードの取得 | ダイレクトアクセスによる取得 | ノードウォーキングによる取得 | ノード属性の取得 | ノード属性の設定 | ノードの追加 | ノードの削除