getElementById Method --------------------------------------------------------------------------------Returns a reference to the first object with the specified value of the ID attribute.SyntaxoElement = document.getElementById(sIDValue)ParameterssIDValue Required. String that specifies the value of an ID attribute. Return ValueReturns the first object with the same ID attribute as the specified value.ResIf the ID value belongs to a collection, the getElementById method returns the first object in the collection.-------------------------------------------------------------------From above, We can see that the method(document.getElementById) can only get ONE DOM element.Try Other Methods.
document.getElementsByTagName() document.getElementsByName()These tow method can return the collection you want.
document.getElementsByTagName() document.getElementsByName() document.getElementById()Notice the letter "s" follows Element.
自己写一个getElementsById <input id="id1" value=1> <input id="id0" value=2> <input id="id1" value=3> <input name="id1" value=4><script> function document.getElementsById(sid){ var obja=new Array(); var objs=document.getElementsByName(sid); for(i=0;i<objs.length;i++) if(objs[i].id==sid) {obja.length++;obja[obja.length-1]=objs[i];} return obja; } id1s=document.getElementsById("id1"); for(i=0;i<id1s.length;i++) alert(id1s[i].value) </script>
function selectAll()
{
var selectedItems
selectedItems = document.all.myCheck
if(selectedItems == null ) return false
if(selectedItems.length == null) selectedItems.checked = blnSelected
for(var i=0;i<selectedItems.length;i++) selectedItems[i].checked = blnSelected
blnSelected = !blnSelected
}
d=document.getElementsByTagName("INPUT")
for(i=0;i<d.length;i++)
{
if(d[i].id=="myCheck")
{
//做你要处理的事情
}
}也可以这样:
d=document.getElementsByName("myCheckName") //这里用input type=checkbox的name属性
for(i=0;i<d.length;i++)
{
//做你要处理的事情
}
--------------------------------------------------------------------------------Returns a reference to the first object with the specified value of the ID attribute.SyntaxoElement = document.getElementById(sIDValue)ParameterssIDValue Required. String that specifies the value of an ID attribute. Return ValueReturns the first object with the same ID attribute as the specified value.ResIf the ID value belongs to a collection, the getElementById method returns the first object in the collection.-------------------------------------------------------------------From above, We can see that the method(document.getElementById) can only get ONE DOM element.Try Other Methods.
document.getElementsByName()These tow method can return the collection you want.
document.getElementsByName()
document.getElementById()Notice the letter "s" follows Element.
<input id="id1" value=1>
<input id="id0" value=2>
<input id="id1" value=3>
<input name="id1" value=4><script>
function document.getElementsById(sid){
var obja=new Array();
var objs=document.getElementsByName(sid);
for(i=0;i<objs.length;i++)
if(objs[i].id==sid)
{obja.length++;obja[obja.length-1]=objs[i];}
return obja;
}
id1s=document.getElementsById("id1");
for(i=0;i<id1s.length;i++)
alert(id1s[i].value)
</script>