`

getElementsByName无法获得Div

阅读更多

<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>

document.getElementsByName("test").length returns 4
document.getElementsByName("myName").length returns 0


why?答案是:div标签根本就没有name属性;所以通过getElementsByName()是得不到div的!


look the DOM documentation:
引用

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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics