这篇翻译不完整。请帮忙从英语翻译这篇文章。
Element.getElementsByTagName()
方法返回一个动态的包含所有指定标签名的元素的HTML集合HTMLCollection
。指定的元素的子树会被搜索,包括元素自己。返回的列表是动态的,这意味着它会随着DOM树的变化自动更新自身。所以,使用相同元素和相同参数时,没有必要多次的调用Element.getElementsByTagName()
.
如果是HTML文档中的某个元素调用了getElementsByTagName函数,
运行前会将参数转为小写字母形式。故不建议在驼峰式命名的SVG元素中使用。 Element.getElementsByTagNameNS()
适用于那种情况.
Element.getElementsByTagName
和 Document.getElementsByTagName()
类似,除了它的搜索被限制为指定元素的后代。
语法
elements = element.getElementsByTagName(tagName)
elements
is a liveHTMLCollection
of found elements in the order they appear in the sub-tree. If no elements were found, theHTMLCollection
is empty.element
is the element from where the search should start. Note that only the descendants of this element are included in the search, but not the element itself.tagName
is the qualified name to look for. The special string"*"
represents all elements. For compatibility with XHTML, lower-case should be used.
实例
// check the alignment on a number of cells in a table. var table = document.getElementById("forecast-table"); var cells = table.getElementsByTagName("td"); for (var i = 0; i < cells.length; i++) { var status = cells[i].getAttribute("data-status"); if ( status == "open" ) { // grab the data } }
参考
Specification | Status | Comment |
---|---|---|
DOM Element.getElementsByTagName() |
Living Standard | Changed the return value from NodeList to HTMLCollection |
Document Object Model (DOM) Level 3 Core Specification Element.getElementsByTagName() |
Recommendation | No change from Document Object Model (DOM) Level 2 Core Specification |
Document Object Model (DOM) Level 2 Core Specification Element.getElementsByTagName() |
Recommendation | No change from Document Object Model (DOM) Level 1 Specification |
Document Object Model (DOM) Level 1 Specification Element.getElementsByTagName() |
Recommendation | Initial definition |
浏览器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 [2] | (Yes) [1] | 5.5 | (Yes) [2] | (Yes) [2] |
getElementsByTagName("*") | 1.0 | (Yes) | 6.0 | (Yes) | (Yes) |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) [1] | (Yes) | (Yes) | (Yes) |
[1] Firefox 19之前的版本,该方法会返回一个 NodeList
;之后根据规范进行了改变。
[2] 最初,该方法会返回一个NodeList
;之后根据规范进行了改变。