事件对象上的QuerySelector是否搜索DOM?

发布于 2025-01-18 10:15:38 字数 512 浏览 5 评论 0原文

这是更详细的问题。假设您有一个事件处理程序

const eventHandy = (e) =>{
 //do stuff;
}

在 eventHandy 中,您在 e.target 上运行一个 querySelector

const eventHandy = (e) =>{
 e.target.querySelectorAll("div");
}

问题是这个 querySelector 是否对 DOM 或事件对象 e.target 执行查询?

就上下文而言,无需过多讨论细节;我们有一个反应应用程序。我们正在做一些具有可访问性和重点的事情。我们的代码的一部分是寻找可聚焦的元素。从争论的一方面来看,在 React 应用程序中进行 DOM 查询是一种反模式,因此应该不惜一切代价避免。另一方面,有时 DOM 查询是必要的,不应该被忽视。然而,有一部分代码获取事件对象并运行 querySelectorAll.. 这对我们的一些开发人员来说是一个症结所在。

Here's the question in more detail. Say, you have an event handler

const eventHandy = (e) =>{
 //do stuff;
}

Within eventHandy, you run a querySelector on e.target

const eventHandy = (e) =>{
 e.target.querySelectorAll("div");
}

The question is does this querySelector do a query on the DOM or the event object e.target?

For context, and without getting too much into details; we have a react app. We are doing some stuff with accessibility and focus. Part of our code is finding focusable elements. On one side of the debate, doing DOM queries inside of a React app is an anti-pattern, and therefore should be avoided at all cost. On the other side, there are times when DOM queries are necessary and shouldn't be discounted. However, there is a portion of code that gets an event object and runs querySelectorAll.. This is a sticking point for some of our developers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿与i 2025-01-25 10:15:38

QuerySelector在事件对象E.Target上进行查询,并将在目标中找到所有元素,而不是在父文档上找到。

querySelector does a query on the event object e.target and will find all elements within the target, not on the parent document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文