1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>控制div属性</title> 6 <style> 7 #outer{width:500px;margin:0 auto;padding:0;text-align:center;} 8 #div1{width:100px;height:100px;background:black;margin:10px auto;display:block;} 9 </style> 10 <script> 11 var changeStyle = function (elem, attr, value) 12 { 13 elem.style[attr] = value 14 }; 15 window.onload = function () 16 { 17 var oBtn = document.getElementsByTagName("input"); 18 var oDiv = document.getElementById("div1"); 19 var oAtt = ["width","height","background","display","display"]; 20 var oVal = ["200px","200px","red","none","block"]; 21 22 for (var i = 0; i < oBtn.length; i++) 23 { 24 oBtn[i].index = i; 25 oBtn[i].onclick = function () 26 { 27 this.index == oBtn.length - 1 && (oDiv.style.cssText = ""); 28 changeStyle(oDiv, oAtt[this.index], oVal[this.index]) 29 } 30 } 31 }; 32 </script> 33 </head> 34 <body> 35 <div id="outer"> 36 <input type="button" value="变宽" /> 37 <input type="button" value="变高" /> 38 <input type="button" value="变色" /> 39 <input type="button" value="隐藏" /> 40 <input type="button" value="重置" /> 41 <div id="div1"></div> 42 </div> 43 </body> 44 </html>
浙公网安备 33010602011771号