日期:2014-05-16  浏览次数:20387 次

求一简单问题~如何迭代HTML上所有的控件
页面上有很textbox   button
如何迭代这些控件以及判断这些控件是属于button   还是textbox?

<form   name= "form1 ">
<input   type= "button "   name= "button1 "   value= "button1 ">
<input   type= "button "   name= "button2 "   value= "button2 ">
<input   type= "button "   name= "button3 "   value= "button3 ">
<input   type= "text "   name= "text1 ">
<input   type= "text "   name= "text2 ">
<input   type= "text "   name= "text3 ">
</form>

------解决方案--------------------
function getElements()
{
var f = document.getElementsByName( 'form1 ');
for (var i = 0, l = f.length; i < l ; i++ )
{
for (var j = 0, k = f[i].all.length; j < k ;j++ )
{
alert(f[i].all[j].type);
}
}
}
------解决方案--------------------
<html>
<head>
<title> </title>
<script language= "JavaScript ">

window.onload = function()
{
GetNodes(document.body);
}
function GetNodes(parent)
{
var children = parent.children;
for (var i = 0; i < children.length; i++)
{
alert( "Tag Name: " + children[i].tagName +
(children[i].type == undefined ? " " : "\nType: " + children[i].type) +
(children[i].value == undefined ? " " : "\nValue: " + children[i].value));
GetNodes(children[i]);
}
}

</script>
</head>
<body>
<form name= "form1 ">
<input type= "button " name= "button1 " value= "button1 ">
<input type= "button " name= "button2 " value= "button2 ">
<input type= "button " name= "button3 " value= "button3 ">
<input type= "text " name= "text1 ">
<input type= "text " name= "text2 ">
<input type= "text " name= "text3 ">
</form>
<br>
<p> </p>
<button> 按钮 </button>
<ul> <li> rrrewrtqt </li> </ul>
</body>
</html>