element.querySelector()与document.querySelector()不同吗? [英] Is element.querySelector() different from document.querySelector()?

查看:279
本文介绍了element.querySelector()与document.querySelector()不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看mdnquerySelector会在两个部分下弹出,但它们似乎都达到了相同的目的。对于不同的情况,哪一个是理想的? ......或者它们在功能上是否相同?

Looking through the mdn "querySelector" pops up under both sections and yet they both seem to achieve the same ends. Is either one ideal for different situations? ...or are they, basically, functionally the same?

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
https://developer.mozilla.org/en-US/docs/Web/API/Element / querySelector

推荐答案

使用 Element.querySelector()因为与 Document.querySelector();

相比,你引用的是较窄的目标在这两种方式中,您都可以访问 DOM树,但由于起点始终为 document 使用 Document.querySelector()您将完全从根遍历dom,直到子元素匹配为止。

in both ways you'll have access to the DOM tree, but since the starting point is always document using Document.querySelector() you'll be traversing the dom entirely from the root until a child element will match.

On另一方面元素已经是对某个节点的引用,所以查询不会从root开始,随之而来的就是...

On the other hand Element is already a reference to a certain node so the query won't begin from root, with all that comes with it ...

这篇关于element.querySelector()与document.querySelector()不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆