简体   繁体   中英

Dollar sign (“$”) is not a function

I'm unsure of why I'm getting this error, but for some reason jQuery's $ is not being recognised?

jQuery(window).load(function ($) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

NOTE: changing $ to jQuery fixes the issue (so I'm sure jQuery is referenced correctly, am using version 2.1.4), but I would like to continue using $ for semantics.

You are overriding the $ variable inside your function, because you have an argument with the same name.

Remove the $ argument and $ will again refer to the global scoped one, equal to jQuery .

jQuery(window).load(function () {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

You can use a parameter for the handler function passed into load . I suggest the same as Anik Islam Abhi's answer : use another name for the argument. For example e or eventArgs .

Note that you (or others landing here) might actually be trying to use a pattern that makes sure jQuery is available as $ inside certain scope (eg because there may be a conflict with another library also declaring $ in global scope). If that's the case, I suggest something along these lines:

(function($) {
    $(window).load(function () {
        'use strict';

        /* Preloader */
        $(".status").fadeOut();
        $(".preloader").delay(1000).fadeOut("slow");

    }); /* END WIDNOW LOAD */
}(jQuery));

This will wrap all your code inside a function which is executed immediately with jQuery passed in as an argument. Because $ is the name of the argument of that function, you'll know for sure that $ is equal to the global jQuery within that function's scope.

You are overriding event parameter with $

Try like this

jQuery(window).load(function (e) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

Maybe you wanted something like that?

jQuery(document).ready(function ($) {
    'use strict';

    /* Preloader */
    $(".status").fadeOut();
    $(".preloader").delay(1000).fadeOut("slow");

}); /* END WIDNOW LOAD */

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