javascript 如何在 WordPress 网站的 jQuery 插件中删除冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17818242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:44:24  来源:igfitidea点击:

How to remove Conflict in a jQuery plugin in a WordPress website?

javascriptjquery-pluginsconflictjquery

提问by Imran

I am working on a WordPress website. I have few filters of jQuery in it and all are working fine (I initiate these plugins using):

我在一个 WordPress 网站上工作。我有几个 jQuery 过滤器,并且一切正常(我使用这些插件启动):

jQuery(document).ready(function($){
  /*code here*/
});

I am also using a jQuery Plugin "filter.js"which is having conflicting issues. Even when I do not initiate this plugin (only on linking the plugin file to my page) it shows jQuery conflict and I can see in console:

我还使用了一个存在冲突问题的 jQuery 插件“filter.js”。即使我没有启动这个插件(仅在将插件文件链接到我的页面时)它也会显示 jQuery 冲突,我可以在控制台中看到:

Uncaught TypeError: Cannot read property 'fn' of undefined 

You can find plugin source here.

你可以在这里找到插件源。

回答by tsdexter

Try wrapping your code like this:

尝试像这样包装你的代码:

(function($){

    $(document).ready(function(){
         //document ready code here
    });

})(jQuery);

Wrap your filter.js plugin with the same wrapper, ie:

使用相同的包装器包装您的 filter.js 插件,即:

(function($){

    //filter.js code here

})(jQuery);

If that's not working - make sure jQuery is included on the page.

如果这不起作用 - 确保页面中包含 jQuery。

回答by user2615436

Are you using two different versions of JQuery in the same page?

您是否在同一页面中使用两个不同版本的 JQuery?

If so then use

如果是这样,那么使用

<script type="text/javascript">
jQuery.noConflict();
</script>

just after the declaring the jQuery and replace all subsequet $ with jQuery

在声明 jQuery 并用 jQuery 替换所有子序列 $ 之后

Hope it helps

希望能帮助到你