document.getElementsByTagName()方法根据节点返回一个节点列表
<html> <head> <title>getElementByTagName</title> </head> <body> <div id="div1">div1</div> <div id="div1">div2</div> <script type="text/javascript"> var nodelist = document.getElementsByTagName("*"); // document.write(nodelist.length); var len = nodelist.length; for (var i = 0; i < len; i++) { document.write(nodelist[i].nodeName+"<br/>"); if(nodelist[i].nodeName=="DIV"){ document.writeln("==value:"+nodelist[i].childNodes[0].nodeValue+"<br/>"); } } </script> </body> </html>
getElementsByName()通过元素的name属性获取的所有元素
<html> <head> <title>getElementsByName</title> </head> <body> <a name="b" href="#">a</a> <h2 name="b" >b</h2> <span name="b">c</span><br/> <script type="text/javascript"> var nodelist = document.getElementsByName("b"); var len = nodelist.length; // alert(len); for (var i = 0; i < len; i++) { document.write((nodelist[i].nodeName).toLowerCase()+"<br/>"); document.write("==value:"+nodelist[i].childNodes[0].nodeValue+"<br/>"); } </script> </body> </html>
getElementById()根据元素的ID获取对元素的引用