js 批量设置样式的三种方法

一般我们都用css来批量设置样式,现在我们用js也可以批量设置样式:

总结三种方法如下

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
#div1{ width:100px; height:100px; background:#069;}
</style>
<script type="text/javascript">
//第一种使用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>
</head>

<body>
<div id="div1"></div>
</body>
</html>

jhujhujhuhiuhihihihihjihjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj 

博客是本人学习后,保存于博客,如有错误和问题请留言,大家一起讨论。

       

 

转载于:https://www.cnblogs.com/chenyajie/archive/2013/02/25/2932142.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值