<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> Untitled document </title> <script type=" Text/javascript ">/** * Get element * Parameter description according to class name: * 1, ClassName class name * 2, Tag element name default all elements * 3, Elm parent element default Doucment/Document.getelementsbyclassname = function (ClassName, TA G, Elm) {var testclass = new RegExp ("(^|\s)" + ClassName + "(\s|$)"); var tag = Tag | | "*"; var elm = Elm | | Document var elements = (Tag = = "*" && elm.all)? Elm.all:elm.getElementsByTagName (tag); var returnelements = []; var current; var length = Elements.length; For (Var i=0. i<length; i++) {current = Elements[i]; if (Testclass.test (Current.classname)) {Returnelements.push (current); } return returnelements; } window.onload = function () {var divs = Document.getelementSbyclassname ("div"); alert (divs.length); for (var i = 0; i < divs.length i++) {alert (divs[i].id); } </script> </pead> <body> <div class= "div" id= "div1" > </div> <div class= "DIVVVV "Id=" Div2 "> <div class=" div "id=" div21 "> </div> </div> <div class=" div "id=" div3 "> </d Iv> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Here I quote this code not because it is convenient for me to call later, but to find a point I never use the method.
Getelementsbyclassname has three parameters, but the call does not have to pass three parameters, you can only pass the first argument, the other two parameters have a default value.
The original JavaScript function parameters can be the default, which I have never known.