开发者

what is jquery noConflict, why do we need that?

i 开发者_StackOverflow社区have seen a jquery code that contains jQuery.noConflict method.

do not know purpose of it, didnt get the why we need that.

i only understand that it is something related to jquery plugin.


It is especially useful when there are more than one javascript libraries used on a page like jQuery, prototype, etc. The $ character is special in those libraries as in jQuery. For this reason, jQuery.noConflict.

See:

Using jQuery with Other Libraries (Official Docs)

Update:

Once you have used jQuery.noConflict, the control of $ is handed over to other libraries rather than jQuery and in this case, you will have to use jQuery rather than $.

Other alternative that turns out to be especially useful when writing jQuery plugins is something like:

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

In this case though, you can use the $ normally even if other libraries are included on the page.


jQuery (and other frameworks like it) store an alias of the jQuery object in the $ variable. It's just for easy shorthand when you write code.

You could actually write all your code like this jQuery('.selector'); instead of using the dollar sign if you wanted to. The no-conflict mode allows you to use other code (usually another framework) that ALSO stores something in the dollar sign variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜