简体   繁体   中英

jQuery: How can I use “$” instead of “jQuery”?

I have simple jQuery in my site, yet I keep getting this error:

Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function

The error only appears if I use "$" instead of "jQuery".

// This works
jQuery(document).ready(function() {
    jQuery('#pass').keyup( ... );
});

// This doesn't
$(document).ready(function() {
    $('#pass').keyup( ... );
});

Do I need to do anything to use "$"?

You can wrap your code:

(function($) {
    // here $ would be point to jQuery object
    $(document).ready(function() {
        $('#pass').keyup( ... );
    });
})(jQuery);

You probably have jQuery noConflict mode enabled somewhere in your code, see: http://api.jquery.com/jQuery.noConflict/

jQuery.noConflict(); // Stops $ from workng

First, jQuery objects are a lot like arrays, so [object DOMWindow] actually is a jQuery object most likely.

You might have a syntax error, like a missing semicolon, right before the call to $(document) which is making the $ look like a property access.

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