querySelector 与 getElementById [英] querySelector vs. getElementById

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

问题描述

我听说 querySelectorquerySelectorAll 是选择 DOM 元素的新方法.在性能和浏览器支持方面,它们与旧方法 getElementByIdgetElementsByClassName 相比如何?

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 是较新的功能.

getElementByIdquerySelector 得到更好的支持.

getElementById is better supported than querySelector.

querySelectorgetElementsByClassName 得到更好的支持.

querySelector is better supported than getElementsByClassName.

querySelector 可让您查找具有无法用 getElementByIdgetElementsByClassName

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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