admin 管理员组文章数量: 1086019
I'm in a unique situation where I need element.querySelector(selector)
to test against the root element
itself, and return it if it matches.
How would you do this?
Note that element.parentNode.querySelector(selector)
wouldn't work for me since it would amtch against element
's siblings.
I'm in a unique situation where I need element.querySelector(selector)
to test against the root element
itself, and return it if it matches.
How would you do this?
Note that element.parentNode.querySelector(selector)
wouldn't work for me since it would amtch against element
's siblings.
-
@Teemu
.closest()
goes up. – Pointy Commented Mar 24, 2020 at 12:54 -
@Teemu but if
.querySelector()
does find a match, then from that point.closest()
will I think return that found element itself, if.closest()
in the DOM API works like jQuery's. – Pointy Commented Mar 24, 2020 at 13:16
1 Answer
Reset to default 12You could do
let matched = element.matches(selector) && element || element.querySelector(selector);
Tests the element first, which probably would make sense given the behavior of .querySelector()
.
本文标签: javascriptHow to have elementquerySelector() include the root elementStack Overflow
版权声明:本文标题:javascript - How to have element.querySelector() include the root element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744025780a2520603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论