妙味——setStyle()

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div1{width: 100px;height: 100px;background: red;}
</style>
<script>
function setStyle(obj,json){
    var attr = '';
    for(attr in json){
        obj.style[attr]=json[attr];
    };
};

window.onload=function(){
    var oDiv = document.getElementById('div1');

    setStyle(oDiv,{width:'200px',height:'200px',background:'green'});
}
</script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

 

posted @ 2013-12-23 23:14  白小虫  阅读(161)  评论(0编辑  收藏  举报