jQuery selector what's difference between jQuery(“element”) and $(“element”)?

狂风中的少年 提交于 2019-12-20 04:31:23

问题


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.

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.

How can I solve it?


回答1:


$ is just a short reference to the global jQuery object.

window.$ === window.jQuery // true

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 ));

By invoking that anonymous method with the jQuery object as argument, we can access it within the method via $.




回答2:


@jAndy provides a good explanation about $ and jQuery in general.

However for your specific problem, it seems that the plugin somehow makes $ unavailable, which either means that it overwrites $ with something else (unlikely) or that it calls jQuery.noConflict().

If it does, have a look why it is doing this. Maybe it includes another library that needs $ for its own working.

Usually, plugins should never assume that $ is available for them. @jAndy showed how to use $ if only jQuery is available.

In addition if you put all your own code in the ready handler, the first argument passed is the global jQuery object, so you can name the parameter as you want:

jQuery(function($) {
    // your code here
}); 


来源:https://stackoverflow.com/questions/6608002/jquery-selector-whats-difference-between-jqueryelement-and-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!