简体   繁体   中英

jQuery + WordPress: $ is not defined

The following code is having some problem with the jQuery.

<script type="text/javascript">
        $(window).load(function() {
            ..
        });
    </script>

If I include jQuery in the document head and then use the above code, it will work fine. However, if I use include jQuery using the Wordpress function wp_enqueue_script , I get following error: $ is not defined . I can see from the page source that the jQuery is loaded properly in the header. How can I fix it? Thanks.

Use the word jQuery instead of $ and pass it a $ reference

jQuery(function($) {
    // you can use $ here
    $(window).load(function() {
        ..
    });
});

hey: Use jQuery with a capital Q instead of $ to make it work. Wordpress usually includes a script which calls jQuery.noConflict() at the end, leaving $ undefined.

Okies if someone don't like this answer comment before downvote please :)

jQuery(function($) { //jQuery passed in as first param, so you can use $ inside
   // use $
});

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