DOM更改Style

24

你需要先获取到元素:

document.querySelector('.box');

然后通过这样的代码来更改样式:

box.style.属性名 = '数据';
box.style.width = '200px';

但是如果遇到background-color这样的属性名又该怎么办呢?使用小驼峰命名法!就像这样:

box.style.backgroundColor = "red";

如果直接使用background-color的话,-会被识别为运算符号。