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.