Javascript $ 不是函数 - jQuery 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3931529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:41:42  来源:igfitidea点击:

$ is not a function - jQuery error

javascriptjquery

提问by Alex

I have the jQuery loaded fine, I've quadruple-checked, though I'm getting this error in FireBug "$ is not a function" and my code doesn't work.

我的 jQuery 加载得很好,我已经进行了四重检查,但我在 FireBug 中收到此错误“$ 不是函数”并且我的代码不起作用。

Here's my code:

这是我的代码:

<script type="text/javascript">
    $("ol li:nth-child(1)").addClass('olli1');
    $("ol li:nth-child(2)").addClass("olli2");
    $("ol li:nth-child(3)").addClass("olli3");
    $("ol li:nth-child(4)").addClass("olli4");
    $("ol li:nth-child(5)").addClass("olli5");
    $("ol li:nth-child(6)").addClass("olli6");
    $("ol li:nth-child(7)").addClass("olli7");
    $("ol li:nth-child(8)").addClass("olli8");
    $("ol li:nth-child(9)").addClass("olli9");
    $("ol li:nth-child(10)").addClass("olli10");
    $("ol li:nth-child(11)").addClass("olli11");
    $("ol li:nth-child(12)").addClass("olli12");
    $("ol li:nth-child(13)").addClass("olli13");
    $("ol li:nth-child(14)").addClass("olli14");
    $("ol li:nth-child(15)").addClass("olli15");
    $("ol li:nth-child(16)").addClass("olli16");
    $("ol li:nth-child(17)").addClass("olli17");
    $("ol li:nth-child(18)").addClass("olli18");
    $("ol li:nth-child(19)").addClass("olli19");
    $("ol li:nth-child(20)").addClass("olli20"); 
</script>

回答by Nick Craver

In Wordpress jQuery.noConflict()is called on the jQuery file it includes (scroll to the bottom of the file it's including for jQuery to see this), which means $doesn't work, but jQuerydoes, so your code should look like this:

在 WordpressjQuery.noConflict()中,它包含的 jQuery 文件被调用(滚动到它包含的文件底部,让 jQuery 看到这一点),这意味着$不起作用,但jQuery确实如此,因此您的代码应如下所示:

<script type="text/javascript">
  jQuery(function($) {
    for(var i=0; i <= 20; i++) 
      $("ol li:nth-child(" + i + ")").addClass('olli' + i);
  });
</script>

回答by strager

It's really hard to tell, but one of the 9001 ads on the page may be clobbering the $object.

真的很难说,但页面上的 9001 条广告之一可能正在破坏该$对象。

jQuery provides the global jQueryobject (which ispresent on your page). You can do the following to "get" $back:

jQuery提供全局jQuery对象(这目前在页面上)。您可以执行以下操作以“取回” $

jQuery(document).ready(function ($) {
    // Your code here
});

If you think you're having jQuery problems, please use the debug (non-production) versions of the library.

如果您认为自己遇到了 jQuery 问题,请使用该库的调试(非生产)版本。

Also, it's probably not best to be editing a live site like that ...

此外,像这样编辑实时站点可能不是最好的......

回答by Roshan Padole

<script type="text/javascript">
    $("ol li:nth-child(1)").addClass('olli1');
    $("ol li:nth-child(2)").addClass("olli2");
    $("ol li:nth-child(3)").addClass("olli3");
    $("ol li:nth-child(4)").addClass("olli4");
    $("ol li:nth-child(5)").addClass("olli5");
    $("ol li:nth-child(6)").addClass("olli6");
    $("ol li:nth-child(7)").addClass("olli7");
    $("ol li:nth-child(8)").addClass("olli8");
    $("ol li:nth-child(9)").addClass("olli9");
    $("ol li:nth-child(10)").addClass("olli10");
    $("ol li:nth-child(11)").addClass("olli11");
    $("ol li:nth-child(12)").addClass("olli12");
    $("ol li:nth-child(13)").addClass("olli13");
    $("ol li:nth-child(14)").addClass("olli14");
    $("ol li:nth-child(15)").addClass("olli15");
    $("ol li:nth-child(16)").addClass("olli16");
    $("ol li:nth-child(17)").addClass("olli17");
    $("ol li:nth-child(18)").addClass("olli18");
    $("ol li:nth-child(19)").addClass("olli19");
    $("ol li:nth-child(20)").addClass("olli20"); 
</script>

change this to

将此更改为

    <script type="text/javascript">
        jQuery(document).ready(function ($) {
        $("ol li:nth-child(1)").addClass('olli1');
        $("ol li:nth-child(2)").addClass("olli2");
        $("ol li:nth-child(3)").addClass("olli3");
        $("ol li:nth-child(4)").addClass("olli4");
        $("ol li:nth-child(5)").addClass("olli5");
        $("ol li:nth-child(6)").addClass("olli6");
        $("ol li:nth-child(7)").addClass("olli7");
        $("ol li:nth-child(8)").addClass("olli8");
        $("ol li:nth-child(9)").addClass("olli9");
        $("ol li:nth-child(10)").addClass("olli10");
        $("ol li:nth-child(11)").addClass("olli11");
        $("ol li:nth-child(12)").addClass("olli12");
        $("ol li:nth-child(13)").addClass("olli13");
        $("ol li:nth-child(14)").addClass("olli14");
        $("ol li:nth-child(15)").addClass("olli15");
        $("ol li:nth-child(16)").addClass("olli16");
        $("ol li:nth-child(17)").addClass("olli17");
        $("ol li:nth-child(18)").addClass("olli18");
        $("ol li:nth-child(19)").addClass("olli19");
        $("ol li:nth-child(20)").addClass("olli20"); 
     });
    </script>

回答by Luillyfe

In my case I was using jquery on my typescript file:

就我而言,我在打字稿文件上使用 jquery:

import * as $ from "jquery";

But this line gives me back an Object $and it does not allow to use as a function (I can not use $('my-selector')). It solves my problem this lines, I hope it could help anyone else:

但是这条线给了我一个对象$,它不允许用作函数(我不能使用$('my-selector'))。它解决了我的问题,我希望它可以帮助其他人:

import * as JQuery from "jquery";
const $ = JQuery.default;

回答by mkoistinen

As RPM1984 refers to, this is mostly likely caused by the fact that your script is loading before jQuery is loaded.

正如 RPM1984 所指,这很可能是由于您的脚本在加载 jQuery 之前加载的事实造成的。

回答by Mayank Dudakiya

There are quite lots of answer based on situation.

根据情况有很多答案。

1) Try to replace '$'with "jQuery"

1)尝试用“jQuery”替换“$

2) Check that code you are executed are always below the main jquery script.

2) 检查您执行的代码是否始终位于主 jquery 脚本下方。

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){

});
</script>

3) Pass $ into the function and add "jQuery" as a main function like below.

3) 将 $ 传递给函数并添加“jQuery”作为主函数,如下所示。

<script type="text/javascript">
jQuery(document).ready(function($){

});
</script>

回答by Sarfraz

That error kicks in when you have forgot to include the jQuery library in your page or there is conflict between libraries - for example you be using any other javascript library on your page.

当您忘记在页面中包含 jQuery 库或库之间存在冲突时,就会出现该错误 - 例如,您在页面上使用任何其他 javascript 库。

Take a look at this for more info:

看看这个了解更多信息:

回答by Mic

When jQuery is not present you get $ is undefinedand not your message.

当 jQuery 不存在时,您会得到$ is undefined而不是您的消息。

Did you check if you don't have a variable called $ somewhere before your code?
Inspect the value of $ in firebug to see what it is.

您是否检查过在代码之前的某个地方是否没有名为 $ 的变量?
检查 firebug 中 $ 的值,看看它是什么。

Slightly out of the question, but I can't resist to write a shorter code to your class assignment:

有点不可能,但我忍不住为你的课堂作业写了一段较短的代码:

    var i = 1;
    $("ol li").each(function(){
        $(this).addClass('olli' + i++);
    });

回答by Diego Santa Cruz Mendezú

Use jQuery for $. I tried and work.

对 $ 使用 jQuery。我尝试并工作。

回答by Marko

There are two possible reasons for that error:

该错误有两个可能的原因:

  1. your jquery script file referencing is not valid
  2. try to put your jquery code in document.ready, like this:

    $(document).ready(function(){

    ....your code....

    });

  1. 您的 jquery 脚本文件引用无效
  2. 尝试将您的 jquery 代码放在 document.ready 中,如下所示:

    $(document).ready(function(){

    ....你的代码....

    });

cheers

干杯