element.getElementsByTagName

4 位贡献者:

这篇翻译不完整。请帮忙从英语翻译这篇文章

Element.getElementsByTagName() 方法返回一个实时的包含具有给出标签名的元素们的HTMLCollection。指定的元素的子树会被搜索,包括元素自己。返回的 list 是实时的,意味着它会随着DOM树的变化自动更新。因此,如果对同一个元素,使用相同的参数,是不需要多次调用Element.getElementsByTagName() 的.

如果是HTML文档中的某个元素调用了getElementsByTagName函数, 运行前会将参数转为小写字母形式。故不建议在驼峰式命名的SVG元素中使用。 Element.getElementsByTagNameNS() works in that case.

Element.getElementsByTagNameDocument.getElementsByTagName()类似,除了它的搜索被限制为指定元素的后代。

语法

elements = element.getElementsByTagName(tagName)
  • elements is a live HTMLCollection of found elements in the order they appear in the sub-tree. If no elements were found, the HTMLCollection 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 
    }
}

Specifications

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;之后根据规范进行了改变。

文档标签和贡献者

标签: 
向此页面作出贡献: auver, teoli, ziyunfei, 最澄
最后编辑者: auver,
隐藏侧边栏