Element:getElementsByTagNameNS() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

Element.getElementsByTagNameNS() 方法返回一个对给定命名空间中具有给定标签名称的元素的实时 HTMLCollection。它与 Document.getElementsByTagNameNS 类似,但其搜索仅限于指定元素的后代。

语法

js
getElementsByTagNameNS(namespaceURI, localName)

参数

namespaceURI

要查找的元素的命名空间 URI(请参阅 Element.namespaceURIAttr.namespaceURI)。例如,如果您需要查找 XHTML 元素,请使用 XHTML 命名空间 URI http://www.w3.org/1999/xhtml

localName

要查找的元素的本地名称,或者特殊值 "*",它匹配所有元素(请参阅 Element.localNameAttr.localName)。

返回值

一个动态的 HTMLCollection,包含了按在树中出现的顺序排列的已找到的元素。

示例

js
// Check the alignment on a number of cells in a table in an XHTML document.
const table = document.getElementById("forecast-table");
const cells = table.getElementsByTagNameNS(
  "http://www.w3.org/1999/xhtml",
  "td",
);

for (const cell of cells) {
  const axis = cell.getAttribute("axis");
  if (axis === "year") {
    // Grab the data
  }
}

规范

规范
DOM
# dom-element-getelementsbytagnamens

浏览器兼容性