function addClass(id,new_class){
var i,n=0;
new_class=new_class.split(",");
for(i=0;i<new_class.length;i++){
if((" "+document.getElementById(id).className+" ").indexOf(" "+new_class[i]+" ")==-1){
document.getElementById(id).className+=" "+new_class[i];
n++;
}
}
return n;
}
现代浏览器有classList属性:
var addClass = function(_element, _classes) {
var classList, item, _i, _len;
classList = _element.classList;
for (_i = 0, _len = _classes.length; _i < _len; _i++) {
item = _classes[_i]; classList.add(item);
}
return _element;
};
时间: 2024-10-07 06:39:19