js如何改变css样式

发布时间:2022-05-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js如何改变css样式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

js改变css样式的方法:1、使用cssText方法;2、使用【setPRoPErty()】方法;3、使用css属性对应的style属性。

js如何改变css样式

本教程操作环境:windows7系统、css3版,DELL G3脑。

js改变css样式的方法:

第一种:用cssText

div.style.cssText='width:600px;height:250px;border:1px red solid;text-align: center;line-height: 250px;';

第二种:用setProperty()

 div.style.setProperty('width','700px');
 div.style.setProperty('height','300px');
 div.style.setProperty('line-height','300px');
 div.style.setProperty('background','#f00');
 div.style.setProperty('color','#fff');
 div.style.setProperty('border','1px solid blue');
 div.style.setProperty('text-align','center');

第三种:使用css属性对应的style属性

 div.style.width = "800px";
 div.style.height = "250px"; 
 div.style.lineHeight = "250px"; 
 div.style.background = "#000";
 div.style.color = "#fff";
 div.style.border = "1px solid #111";
 div.style.textAlign = "center";
<body>
        <div id="div">这是一个盒子</div>
        <script type="text/javascript">
            VAR div = document.getElementById("div");
            
            //第一种:用cssText
            div.style.cssText='width:600px;height:250px;border:1px red solid;text-align: center;line-height: 250px;';
            
            //第二种:用setProperty()
            div.style.setProperty('width','700px');
            div.style.setProperty('height','300px');
            div.style.setProperty('line-height','300px');
            div.style.setProperty('background','#f00');
            div.style.setProperty('color','#fff');
            div.style.setProperty('border','1px solid blue');
            div.style.setProperty('text-align','center');
            
            //第三种:使用css属性对应的style属性
            div.style.width = "800px";
            div.style.height = "250px"; 
            div.style.lineHeight = "250px"; 
            div.style.background = "#000";
            div.style.color = "#fff";
            div.style.border = "1px solid #111";
            div.style.textAlign = "center";
        </script>
    </body>

相关教程推荐:CSS视频教程

以上就是js如何改变css样式的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的js如何改变css样式全部内容,希望文章能够帮你解决js如何改变css样式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:jsjs