js操作元素的样式

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'    //一定要记得+空格

猜你喜欢

转载自www.cnblogs.com/zhaodz/p/11617779.html