TypeError:jQuery(...)。ready(...)不是函数

时间:2014-02-13 13:17:41

标签: javascript jquery

好的,我知道之前已经问过这个问题但是没有一个答案似乎适用于我的情况。我正在尝试运行一小段jQuery(我刚开始使用它)。

jQuery(document).ready(function(){
    jQuery('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
})();

我在FF中收到错误TypeError: jQuery(...).ready(...) is not a function或在Chrome中收到Uncaught TypeError: object is not a function

  • 解决方案1是用$替换jQuery,但我显然已经如上所示那样做了
  • 我不在Wordpress中
  • 我只使用jQuery和上面的迷你脚本,没有其他JS
  • jQuery本身似乎很好 enter image description here

我在这里缺少什么?

5 个答案:

答案 0 :(得分:34)

尝试在doc ready结束时删除此();

jQuery(document).ready(function(){
  jQuery('.comtrig').on('click',function(){
    $(this).next().animate({'display':'inline'},1000);
  });
}); //<----remove the (); from here

();通常用于拥有立即调用的函数表达式(IIFE),它具有如下语法:

(function(){
   // your stuff here
})(); //<----this invokes the function immediately.

您的错误:

in firefox = TypeError: jQuery(...).ready(...) is not a function

in chrome = Uncaught TypeError: object is not a function

,因为:

您的文档就绪处理程序不是自动执行匿名函数

答案 1 :(得分:5)

代码中有两个问题。

1 - 代码末尾的括号。

2 - $(this)应该是jQuery(this)或$ inside函数。

jQuery(document).ready(function($){
    $('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
});

答案 2 :(得分:3)

删除末尾的额外括号()。保留代码如下。

jQuery(document).ready(function(){
    jQuery('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
}); // <== remove () from here

答案 3 :(得分:0)

以这种方式传递jquery对象对我有用。

$(document).ready(function () {
    console.log("jquery");
}(jQuery));

答案 4 :(得分:0)

这些错误:

    TypeError: jQuery(...).ready(...) is not a function
    or
    Uncaught TypeError: object is not a function
如果你在代码之后实现Jquery库,也会发生

,它应该在之前,在这里订购

 <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>