码迷,mamicode.com
首页 > Web开发 > 详细

js操作元素的样式

时间:2019-10-02 18:57:56      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:height   命名法   nbsp   name   log   color   OLE   ons   元素   

1.元素.style.属性=‘属性值‘

var div=document.getElementById(box)
div.style.width=300px;
div.style.height=300px;
div.style.backgroundColor=red;   //css中有-的属性,在js中要改成驼峰命名法

2.div.style.属性     获取元素的样式

var div=document.getElementById(box)
console.log(div.style.width);
console.log(div.style.height);
console.log(div.style.backgroundColor);   //如果要通过style获取样式,只能获取行内样式

3.通过操作类,改变元素的样式

var div=document.getElementById(box)
div.className +=  one//一定要记得+空格

 

js操作元素的样式

标签:height   命名法   nbsp   name   log   color   OLE   ons   元素   

原文地址:https://www.cnblogs.com/zhaodz/p/11617779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!