getElementsByName
getElementsByName;getElementsByTagName的放回值为数组,遍历的时候不能用for(var v in arrs) 因为用这种方式遍历的是key
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function bntClick() {
var inputname = document.getElementsByName("bt1");//按照表单的name来取得元素,这里是取name属性值为bt1的元素
for (var i = 0; i < inputname.length; i++) {
var nam = inputname[i];
alert(nam.value)
}
}
</script>
</head>
<body >
<input type="button" value="1" name="bt1" />
<input type="button" value="2" name="bt1" />
<input type="button" value="3" name="bt1" />
<input type="button" value="4" name="bt1" />
<input type="button" value="5" name="bt1"/>
<input type="button" value="提交" οnclick="bntClick()" />
</body>
</html>
getElementsByTagName
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function intEvent() {
var inputs = document.getElementsByTagName("input"); //按照标签类型来取得元素,这里是按照input标签来取得所有input元素
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
//这里也可以获取input表单的值
//alert(input.value);
input.onclick = dian;
}
}
function dian() {
alert("点了")
}
</script>
</head>
<body οnlοad="intEvent()">
<input type="button" value="1" />
<input type="button" value="2" />
<input type="button" value="3" />
<input type="button" value="4" />
<input type="button" value="5" />
</body>
</html>