时间:2021-07-01 10:21:17 帮助过:4人阅读
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>