サンプル10-属性の操作の例


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>サンプル10-属性の操作の例</title>
<script type="text/javascript">
	window.onload = function(){
	    var targetTag = document.getElementById("targetTag");

		document.getElementById("btnSetAttribute").onclick = function(){
			targetTag.setAttribute("style","background-color: yellow;");
			return false;
		};

		document.getElementById("btnGetAttribute").onclick = function(){
			alert(targetTag.getAttribute("style"));
			return false;
		};

		document.getElementById("btnHasAttribute").onclick = function(){
			alert(targetTag.hasAttribute("style"));
			return false;
		};

		document.getElementById("btnRemoveAttribute").onclick = function(){
			targetTag.removeAttribute("style");
			return false;
		};
	}
</script>
</head>
<body>
<h1>サンプル10-属性の操作の例</h1>
<hr />
<div id="targetTag" style="background-color: gray;">targetTag</div>
<form>
	<button id="btnSetAttribute">属性値の追加・変更</button><br />
	<button id="btnGetAttribute">属性値の取得</button><br />
	<button id="btnHasAttribute">属性値の存在確認</button><br />
	<button id="btnRemoveAttribute">属性の削除</button>
</form>
</body>
</html>