Created
June 25, 2010 05:02
-
-
Save getify/452439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getElementsByClassName(classname,root,tagname) { | |
root = root || document; | |
tagname = tagname || "*"; | |
classname = classname.split(/\s+/); | |
var els = root.getElementsByTagName(tagname), | |
ret=[], i,j=0,k,len = els.length, m, cn, klen=classname.length, reEdgeSpaces = /[\t\n\r\f]/g | |
; | |
for (i=0; i<len; i++) { | |
cn = ' '+els[i].className.replace(reEdgeSpaces,' ')+' '; | |
m = true; | |
for (k=0; k<klen; k++) { | |
if (cn.indexOf(' '+classname[k]+' ')<0) { | |
m = false; | |
break; | |
} | |
} | |
if (m) { | |
ret[j++] = els[i]; | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks to jdalton for many hints/improvements