简体   繁体   English

如何正确解决jquery冲突

[英]how to properly resolve jquery conflict

I previously had a problem with jquery conflicting with jquery in the index page of a document and the jquery in the following function 我以前遇到过jquery与文档索引页中的jquery和以下函数中的jquery冲突的问题

    (function (window, document) {

...jquery code not necessary and far to long

})(window, document);

I have tried resolving Jquery conflict with the following code 我尝试使用以下代码解决Jquery冲突

     var jQuery132 = $.noConflict(true);
    (function (window, document) {

    .....

})(jQuery132)(window, document);

however this doesn't work for resolving the conflict and the function fails.I think it has something to do with the following part 但是,这不适用于解决冲突并且功能失败。我认为这与以下部分有关

})(jQuery132)(window, document);

How do use jquery noConflict with this type of function? 如何将jQuery noConflict与此类函数一起使用? Thanks 谢谢

It should be something like this: 应该是这样的:

jQuery.noConflict();

(function (window, document, $) {

   ...

})(window, document, jQuery);

With jQuery being passed as a parameter to the IIFE. jQuery作为参数传递给IIFE。 This effectively lets you use the $ or anything else really ( if you want to use jQuery123 go ahead ) without conflicting inside of the closure generated by the IIFE. 这有效地使您可以真正使用$或其他任何东西(如果您想使用jQuery123继续前进),而不会在IIFE生成的闭包内部发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM