简体   繁体   English

美元符号(“$”)不是一个功能

[英]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的$不被识别?

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. 注意:将$更改$ jQuery解决问题(所以我确定jQuery被正确引用,使用的是2.1.4版本),但我想继续使用$语义。

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

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 . 可以将参数用于传递给load的处理函数。 I suggest the same as Anik Islam Abhi's answer : use another name for the argument. 我建议和Anik Islam Abhi的回答一样 :使用另一个名字作为论点。 For example e or eventArgs . 例如eeventArgs

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). 请注意,您(或其他登陆此处)可能实际上正在尝试使用一种模式,以确保jQuery在某个范围内可用作$ (例如,因为可能与另一个库发生冲突,同时在全局范围内声明$ )。 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. 这将把你的所有代码包装在一个函数中,该函数在作为参数传入的jQuery中立即执行。 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. 因为$是该函数的参数的名称,所以您肯定知道$等于该函数范围内的全局jQuery

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 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM