最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dollar sign ("$") is not a function - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jun 13, 2015 at 10:00 Jeroen 63.7k46 gold badges228 silver badges365 bronze badges asked Jun 13, 2015 at 9:55 IljaIlja 46.5k103 gold badges289 silver badges526 bronze badges 5
  • 4 Your parameter is a $ – Ed Heal Commented Jun 13, 2015 at 9:58
  • 1 Rename your parameter, problem solved. Happy life carries on. – TaoPR Commented Jun 13, 2015 at 10:02
  • 2 $ is the event. You could pass it to ready method but not load method. This would be for example to handle $ in wordpress jQuery(document).ready(function($){...}); – A. Wolff Commented Jun 13, 2015 at 10:20
  • This is a duplicate from stackoverflow.com/questions/12343714/… I believe – xarlymg89 Commented Jul 24, 2019 at 11:03
  • Possible duplicate of TypeError: $ is not a function when calling jQuery function – xarlymg89 Commented Jul 24, 2019 at 11:04
Add a comment  | 

5 Answers 5

Reset to default 9

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 (e.g. 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 */
$ = jQuery; 

can work too. Not the best way though.

var $ = jQuery.noConflict();

Leave it at the top of the js file.

发布评论

评论列表(0)

  1. 暂无评论