简体   繁体   中英

jQuery addClass is not working now

So I'm trying to add a class with jquery. Here is the code:

jQuery(document).ready(function($) {
    $('body').addClass('scrollbar-dynamic');
});

I can confirm the script is loaded on frontend as it appears in source code. Plus all other javascript "1400 lines" works fine. The addClass jquery method is also used about 30 more times throughout the script. I'm also using the latest version of wordpress It's just this time it's not working. Here is a working js fiddle

Stuff like this just drives me crazy. Cheers for the help

Ok - since console.log(jQuery) is returning what we'd expect, I think the issue is with how the library is being referenced. Here's your code:

jQuery(document).ready(function($) {
  $('body').addClass('scrollbar-dynamic');
});

The script knows what jQuery is...but it doesn't seem to know what $ is, and it's not being bound to anything when it's called.

Try this - if it works, it's a binding issue.

jQuery(document).ready(function($) {
  jQuery('body').addClass('scrollbar-dynamic');
});

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