
一般我们都用css来批量设置样式,现在我们用js也可以批量设置样式:
总结三种方法如下
代码如下:
无标题文档 //第一种使用JSON
function setStyle(obj,json){
for(var i in json)
{
obj.style[i]=json[i];
}
}
window.onload=function(){
var oDiv=document.getElementById('div1');
// setStyle(oDiv, {width: '200px', background: 'red'}); //第一种
// oDiv.style.cssText="width: 200px; height:300px; background:yellow;"; //第二种 使用cssText
//使用第三种 with (不推荐使用)
with(oDiv.style)
{
width='300px';
height='500px';
background='yellow';
}
};
script>