jQuery(“#id”)或jQuery(document.getElementById(" id"))? [英] jQuery("#id") or jQuery(document.getElementById("id"))?

查看:93
本文介绍了jQuery(“#id”)或jQuery(document.getElementById(" id"))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

性能差异显示在此处

我应该使用 jQuery(#id) jQuery(document.getElementById(id))

我是否应该更改 jQuery(#id)的所有用途 document.getElementById(id)因为性能差异?

Should I change all of my uses of jQuery("#id") to document.getElementById("id") because of the performance difference?

我更喜欢 jQuery(#id)因为它更易于阅读并与CSS选择器保持一致。

I prefer jQuery("#id") because it is easier to read and consistent with the CSS selector.

推荐答案

老实说,这取决于你。

一个主观问题这样会给你一个主观的答案,或者,最好是一个深思熟虑的每个人的利弊列表,并在最后提出建议。

A subjective question like this is going to get you a subjective answer, or, at best, a well thought-out list of pros and cons for each with a recommendation at the end.

不过我会告诉你这个问题:使用 $(#id)如果您打算使用它,可以节省很多很多时间再看一遍:看看其中的字符数量与 $(document.getElementById(id))之间的差异。

I'll tell you this, though: using $(#id) saves you a lot of time if you're going to use it over and over again: look at the difference between the amount of characters in that and in $(document.getElementById(id)).

为此目的单独使用jQuery ,但是如果你已经在使用它并且你将要引用DOM操作的对象很多,你也可以选择省钱打字的方式。

Don't use jQuery solely for this purpose, but if you're already using it and you're going to be referring to objects for DOM manipulation a lot, you might as well go the way that saves you some typing.

这篇关于jQuery(“#id”)或jQuery(document.getElementById(" id"))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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