javascript - getElementsByTagName() 是否仅返回完全匹配?

标签 javascript

我认为它应该只返回完全匹配。

我在我必须使用的源代码中看到类似的内容:

var someArray = someObject.getElementsByTagName("item");

除了当我检查 DOM 时,我没有看到任何名为“items”的标签。 有一些 css 类“.some_item_details”。

随后我看到一个关于元素为空的错误,这对我来说是有意义的。让我烦恼的是我在生产代码库中看到了这一点。所以我在想,“肯定没有人会犯这样的事,我一定错过了一些东西”。 “item”不是必须存在自定义标签吗?!?

最佳答案

检查getElementsByTagName()

确实,getElementsByTagName matches 会产生精确匹配。

Returns an HTMLCollection of elements with the given tag name. The complete document is searched, including the root node. The returned HTMLCollection is live, meaning that it updates itself automatically to stay in sync with the DOM tree without having to call document.getElementsByTagName() again.

Note:

document.getElementsByTagName() is similar to element.getElementsByTagName(), except that its search encompasses the whole document.

@NiettheDarkAbsol 的评论:

var someArray = someObject.getElementsByTagName("item");

Indeed, the code snippet you have shown would be searching for item tags. This might be okay if someObject is an XML context.

关于javascript - getElementsByTagName() 是否仅返回完全匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24869804/

相关文章:

javascript - 未处理的 Promise 拒绝 : Template parse errors on Angular 2

java - 实现复制和粘贴的代码/文本检测?

javascript - 如何强制更改事件发生在选择元素上

javascript - 用js表格增值

javascript - 在链接前插入文字

javascript - 如何将整个数组添加到 useState 变量

Javascript - .getHours() 不工作

javascript - 当我点击它时,我想让我的图像弹出更大

javascript - 为什么这个 if 条件总是 true (js)?

javascript - 使用 gettext 翻译 .js 文件