问题描述
我听说 querySelector
和 querySelectorAll
是选择 DOM 元素的新方法.在性能和浏览器支持方面,它们与旧方法 getElementById
和 getElementsByClassName
相比如何?
I have heard that querySelector
and querySelectorAll
are new methods to select DOM elements. How do they compare to the older methods, getElementById
and getElementsByClassName
in terms of performance and browser support?
与使用 jQuery 的查询选择器相比,性能如何?
How does the performance compare to using jQuery's query selector?
是否有关于使用哪种方法的最佳实践建议?
Is there a best practice recommendation for which method to use?
推荐答案
更好"是主观的.
querySelector
是较新的功能.
getElementById
比 querySelector
得到更好的支持.
getElementById
is better supported than querySelector
.
querySelector
比 getElementsByClassName
得到更好的支持.
querySelector
is better supported than getElementsByClassName
.
querySelector
可让您查找具有无法用 getElementById
和 getElementsByClassName
querySelector
lets you find elements with rules that can't be expressed with getElementById
and getElementsByClassName
您需要为任何给定的任务选择合适的工具.
You need to pick the appropriate tool for any given task.
(在上面,对于 querySelector
阅读 querySelector
/querySelectorAll
).
(In the above, for querySelector
read querySelector
/ querySelectorAll
).
这篇关于querySelector 与 getElementById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!