这是使用 javascript 更改样式以分配样式值的示例
CSS
div {
width: 30px;
height: 30px;
}
HTML
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
Javascript
var elements = document.getElementsByTagName("div");
var length = elements.length;
var element;
for (var i = 0; i < length; i += 1) {
element = elements[i];
element.style.borderStyle = "solid";
element.style.borderWidth = "2px";
element.style.borderColor = "green";
}
在jsfiddle 上
虽然同样可以通过 CSS 定义来实现
CSS
div {
width: 30px;
height: 30px;
border-style: solid;
border-width: 2px;
border-colour: green;
}
HTML
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
在jsfiddle 上