1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="Generator" content="EditPlus®"> 6 <meta name="Author" content=""> 7 <meta name="Keywords" content=""> 8 <meta name="Description" content=""> 9 <title>获取DOM元素的三种方法</title> 10 </head> 11 <body onload="getElement()"> 12 <script type="text/javascript"> 13 function getElement(){ 14 //getElementsByTagName根据标签名 15 var a=document.getElementsByTagName("input") 16 alert(a.length)//4 17 //getElementsByName根据name值 18 var b=document.getElementsByName("aihao") 19 alert(b.length)//3 20 //getElementById根据id值 21 var c=document.getElementById("c") 22 alert(c.value) 23 24 } 25 </script> 26 <input type="checkbox" name="aihao" value="001">抽烟<br> 27 <input type="checkbox" name="aihao" value="002">喝酒<br> 28 <input type="checkbox" name="aihao" value="003">烫头<br> 29 <input type="text" id="c" name="c" value="004"> 30 31 </body> 32 </html>