首页 > 代码库 > 控制div属性

控制div属性

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<title>控制div属性</title>
<link rel="stylesheet" href="" type="text/css">
<script type="text/javascript" src=""></script>
<style type="text/css">
  #test{
    height:50px;
    width:50px;
    background:black;
  }
</style>
<script type=‘text/javascript‘>
  var testStyle = function(elem,attr,value){
    elem.style[attr] = value;
  }
  window.onload = function(){
    var btn = document.getElementsByTagName("button");
    var test = document.getElementById("test");
    var attr = [‘width‘,‘height‘,‘color‘,‘display‘,‘display‘];
    var value = http://www.mamicode.com/[‘100px‘,‘100px‘,‘red‘,‘none‘,‘block‘];
    for(var i=0;i<btn.length;i++){
      btn[i].onclick = function(){
        testStyle(test,attr,value);
      }

    }
}
</script>
</head>
<body>
  <button>变宽</button>
  <button>变高</button>
  <button>变色</button>
  <button>隐藏</button>
  <button>重置</button>
  <div id="test"></div>
</body>
</html>