jQuery选择器jQuery("element")和$("element")之间有什么区别? [英] jQuery selector what's difference between jQuery("element") and $("element")?
问题描述
我多次使用过jQuery,但始终使用以下方式:$(document)
.最近我见过很多人使用jQuery(document)
,我不知道它们之间的区别,我以为它们是相同的.
I used jQuery many times ago, but always used like this: $(document)
. Lately i seen many times somebody using jQuery(document)
, I don't know difference between them, I thought they are same.
但是我现在有一个非常棘手的问题.您知道大多数jQuery插件都使用$(document)
方法.现在,我有一个必须使用插件,该插件使用jQuery(document)
.我必须包含它,但是包含之后,我不能再使用$(document)
方法和使用它的插件了.
But I have very hard problem now. You know most of jQuery plugin uses $(document)
method. Now I have one must use plugin that uses jQuery(document)
. I must include that, but after included I can't no longer use $(document)
method and plugins which uses it.
我该如何解决?
推荐答案
$
只是对全局jQuery
对象的简短引用.
$
is just a short reference to the global jQuery
object.
window.$ === window.jQuery // true
大多数插件作者将美元符号放入自调用方法中,以确保美元符号确实在引用jQuery对象.
Most plugin authors make sure that the dollar sign really is referencing the jQuery object, by putting it into a self-invoking method.
(function( $ ) {
// $(document)
}( jQuery ));
通过使用jQuery
对象作为参数调用该匿名方法,我们可以通过$
在该方法内访问它.
By invoking that anonymous method with the jQuery
object as argument, we can access it within the method via $
.
这篇关于jQuery选择器jQuery("element")和$("element")之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!