js改变css样式的方法:1、使用cssText方法;2、使用【setProperty()】方法;3、使用css属性对应的style属性。
本教程操作环境: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";
登录后复制
这是一个盒子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";
登录后复制
相关教程推荐:CSS视频教程
以上就是js如何改变css样式的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2876956.html