javascript——獲取元素方式
阿新 • • 發佈:2018-08-21
func cti 元素 nts 方式 nbsp elements byname ava
//1:依據id //var element = document.getElementById("test"); console.log(element); //2:依據class var elements = document.getElementsByClassName("clazz"); for(var index in elements){ //console.log(elements[index]); } //3:依據name var elements = document.getElementsByName("hobby"); for(var index in elements){ console.log(elements[index]); } //4:依據標簽名字 var elements = document.getElementsByTagName("input"); for(var index in elements){ console.log(elements[index]); }
script在上面定義的方法
<script> function test(){ var elements = document.getElementsByTagName("input"); for(var index in elements){ console.log(elements[index]); } } </script> </head> <body onload="test()"> <input name="hobby" type="checkbox" value="0" />0 <input name="hobby" type="checkbox" value="1" />1 <input name="hobby" type="checkbox" value="2" />2 <input name="hobby" type="checkbox" value="3" />3 </body>
javascript——獲取元素方式