看了一句话,我们都是搬运工,github的搬运工,下面这代码搬运来自各个地方,最后成型。
var classCache = {};
function getElementsByClassName(cls, root,
tag){
var elems, classRE,
rs = [];
root = root ||
document;
tag = tag || ‘*‘;
elems =
root.getElementsByTagName(tag);
//去掉字符串前后空格
cls =
cls.replace(/^\s+|\s+$/g, ‘‘).split(/\s+/g);
cls =
cls.join(‘\\s+[\\S]*\\s*‘);
classRE = classCache[cls] || new
RegExp(‘(^|\\s+)‘ + cls + ‘(\\s+|$)‘);
for(var i = 0, len = elems.length; i
< len; i++){
classRE.test(elems[i].className) &&
rs.push(elems[i]);
}
return rs;
}
自己动手实现getElementsByClassName,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/zuiaitianxibi/p/3737475.html