getElementsByClassName

var getElementsByClassName = function(str,root,tag){ if(root){ root = typeof root == "string" ? document.getElementById(root) : root; } else { root = document.body; } tag = tag || "*"; var els = root.getElementsByTagName(tag),arr = []; for(var i=0,n=els.length;i<n;i++){ for(var j=0,k=els[i].className.split(" "),l=k.length;j<l;j++){ if(k[j] == str){ arr.push(els[i]); break; } } } return arr; } //how to use <div id="demo4-1"> <ul> <li class="demo41_tabMenu">menu1</li> <li class="demo41_tabMenu">menu2</li> <li class="demo41_tabMenu">menu3</li> </ul> </div> // console.log(getElementsByClassName('demo41_tabMenu','demo4-1','li').length); // console.log(getElementsByClassName('demo41_tabMenu','demo4-1').length); // console.log(getElementsByClassName('demo41_tabMenu').length);

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.