サンプル11-属性ノードの操作の例
targetTag
記事へ戻る
---
愚鈍人ホーム
htmlソース
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>サンプル11-属性ノードの操作の例</title>
<script type="text/javascript">
window.onload = function(){
var targetTag = document.getElementById("targetTag");
document.getElementById("btnSetAttribute").onclick = function(){
var style=document.createAttribute("style");
style.nodeValue = "background-color: yellow;";
targetTag.setAttributeNode(style);
return false;
};
document.getElementById("btnGetAttribute").onclick = function(){
var AttributeNode=targetTag.getAttributeNode("style");
if(AttributeNode){
alert(targetTag.getAttributeNode("style").nodeValue);
} else {
alert("targetTagにstyle属性は存在しません");
}
return false;
};
document.getElementById("btnAttributes").onclick = function(){
var attributes=targetTag.attributes;
var msg="";
for(var i=0;i<attributes.length;i++){
msg+=(attributes[i].nodeName+":"+attributes[i].nodeValue)+"\n";
}
alert(msg);
return false;
};
document.getElementById("btnRemoveAttribute").onclick = function(){
targetTag.removeAttributeNode(targetTag.getAttributeNode("style"));
return false;
};
}
</script>
</head>
<body>
<h1>サンプル11-属性ノードの操作の例</h1>
<hr />
<div id="targetTag" style="background-color: gray;">targetTag</div>
<form>
<button id="btnSetAttribute">属性ノードの追加・変更</button><br />
<button id="btnGetAttribute">属性ノードの取得</button><br />
<button id="btnAttributes">全属性ノードの取得</button><br />
<button id="btnRemoveAttribute">属性ノードの削除</button>
</form>
</body>
</html>