//定义getByClassName函数,让函数实现根据class name获取对象并返回
function getByClassName(obj,cls){
var elements = obj.getElementsByTagName("*");
var result = [];
for(var i = 0; i < elemnts.length; i++)
{
if(elements[i].className = cls){
result.push(elements[i]);
}
}
return result;
}
function hasClass( obj, cls ){
return obj.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}
function removeClass( obj, cls ){
if( hasClass( obj, cls )){
//remove
var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
obj.className = obj.className.replace(reg, "");
}
}
//定义getByClassName函数,让函数实现给对象增加class
function addClass(obj, cls){
if(!hasClass(obj,cls)){
obj.className += " " + cls;
}
}
function getByClassName(obj,cls){ var elements = obj.getElementsByTagName("*"); var result = []; for(var i = 0; i < elemnts.length; i++) { if(elements[i].className = cls){ result.push(elements[i]); } } return result; }
遍历的时候单词拼写错误;
elemnts.length少了一个e,改成elements.length就可以了