事件对象上的QuerySelector是否搜索DOM?
这是更详细的问题。假设您有一个事件处理程序
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.