我认为它应该只返回完全匹配。
我在我必须使用的源代码中看到类似的内容:
var someArray = someObject.getElementsByTagName("item");
除了当我检查 DOM 时,我没有看到任何名为“items”的标签。 有一些 css 类“.some_item_details”。
随后我看到一个关于元素为空的错误,这对我来说是有意义的。让我烦恼的是我在生产代码库中看到了这一点。所以我在想,“肯定没有人会犯这样的事,我一定错过了一些东西”。 “item”不是必须存在自定义标签吗?!?
最佳答案
确实,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/