js为元素动态增加class样式
div{ border:1px solid #ccc; width:200px; height:200px;}
.a{ background:#900; }
.b{ font-size:30px; font-weight:bold;}
function addClass(elements, value)
{if (!elements.className) {
elements.className = value;
}
else
{
newClass = elements.className;
newClass += " ";
newClass += value;
elements.className = newClass;
}
}
window.onload = function ()
{
var test = document.getElementById('test');
alert(test.className);
addClass(test, 'a b');
//addClass(test, 'b');
}
<div id="test">这里是测试层</div>