HTML 集合结构在所有浏览器中都相同
HTML collection structure same in all browsers
当您使用 document.getElementsByTagName()
搜索元素时,我们得到 HTMLCollection
。如果元素具有 id
,则输出具有 index 和 id 的元素。
问题是,这个结构会在所有浏览器中保持不变还是会改变?
例子
(function() {
var inputs = document.getElementsByTagName("div")[0].children;
console.log(inputs);
})()
<div id="content">
<input type="text" id="input1" />
<input type="text" id="input2" />
<input type="text" id="input3" />
<input type="text" id="input4" />
</div>
如果有多个元素匹配用作索引的字符串,您不能指望浏览器这样做。
Browser compatibility
Different browsers behave differently when there
are more than one elements matching the string used as an index (or
namedItem's argument). Firefox 8 behaves as specified in DOM 2 and
DOM4, returning the first matching element. WebKit browsers and
Internet Explorer in this case return another HTMLCollection and Opera
returns a NodeList of all matching elements.
来源:https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
当您使用 document.getElementsByTagName()
搜索元素时,我们得到 HTMLCollection
。如果元素具有 id
,则输出具有 index 和 id 的元素。
问题是,这个结构会在所有浏览器中保持不变还是会改变?
例子
(function() {
var inputs = document.getElementsByTagName("div")[0].children;
console.log(inputs);
})()
<div id="content">
<input type="text" id="input1" />
<input type="text" id="input2" />
<input type="text" id="input3" />
<input type="text" id="input4" />
</div>
如果有多个元素匹配用作索引的字符串,您不能指望浏览器这样做。
Browser compatibility
Different browsers behave differently when there are more than one elements matching the string used as an index (or namedItem's argument). Firefox 8 behaves as specified in DOM 2 and DOM4, returning the first matching element. WebKit browsers and Internet Explorer in this case return another HTMLCollection and Opera returns a NodeList of all matching elements.
来源:https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection