简体   繁体   中英

jQuery conflict on wordpress page

I'm fairly new to jQuery and I'm encountering a problem. On my page, I'd like to add a new piece of jQuery code which makes use of:

Somehow, when I add this script, my piece of code works, but the rest of the website code is broken.

I've tried to add the $.noConflict(true); script to my jquery. But it doesn't work somehow.. Can anybody help me out?

Thanks in advance.

Hank

您可以设置另一个变量来代替$,例如var $j = jQuery.noConflict();

You need to create a new variable instead of the $ and use that.

For example for wordpress u will use $

then for the other jquery you could create a new one like this

var jquery = $.noConflict();

For example Now you will use jquery instead of $

jquery(document).ready(function (){
  var form = jquery('#formcomments');
  var submit = jquery('#comments');

Wordpress is in noConflict mode by default, you don't have to add anything, you just have to use jQuery , and not $ .

This is usually handled with jQuery wrappers

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

It also means

$.noConflict(true);

won't work, as $ is not defined in Wordpress, it's all explained in the Codex

Try to convert all $ to jQuery in your code or use:

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

or put your code inside a closure:

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

$.noConflict(); add this function where your scripts is conflict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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