1. 程式人生 > >JavaScript屬性操作1

JavaScript屬性操作1

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			#div1{
				width:240px;
				height: 200PX;
				border: 1px solid #333;
				background: #F1F1F1F;
				padding: 10px;
			}
		</style>
		<script>
			window.onload=function()
			{
				var oAttr=document.getElementById('attr');
				var oVal=document.getElementById('val1');
				var oBtn=document.getElementById('btn1');
				var oDiv=document.getElementById('div1');
				
				oBtn.onclick=function(){
					oAttr.value	//width
					oVal.value  //200px value
					
					oDiv.style[oAttr.value]=oVal.value;
				}
			
			}
		</script>
	</head>
	<style>
		div{
			width:100px;
			height: 100px;
			border: 1px solid red;
			
		}
	</style>
	<body>
		請輸入屬性名稱:<input id="attr" type="text"/>
		請輸入屬性值:<input id="val1" type="text"/>
		<input type="button" value="確定"  id="btn1"/>
		<div id="div1"></div>
	</body>
</html>