<input name="test" type="radio" id="r1">
<input name="test" type="radio" id="r2">
<input name="test" type="radio" id="r3">
<div name="myName" id="Div1"></div>
<div name="myName" id="Div2"></div>
[color=red]document.getElementsByName("test").length returns 4
document.getElementsByName("myName").length returns 0[/color]
why?答案是:div标签根本就没有name属性;所以通过getElementsByName()是得不到div的!
look the DOM documentation:
[quote]
When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.
Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME?A HREF="../properties/expando.html">expando.
[/quote]