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

Rub*_*ert 23 javascript jquery

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

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

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

  • 解决方案1是替换$,jQuery但我显然已经如上所示
  • 我也不在Wordpress中
  • 我只使用jQuery和上面的迷你脚本,没有其他JS
  • jQuery本身似乎加载得很好 在此输入图像描述

我在这里错过了什么?

Jai*_*Jai 37

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

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

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

(function(){
   // your stuff here
})(); //<----this invokes the function immediately.
Run Code Online (Sandbox Code Playgroud)

你的错误:

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

在chrome = Uncaught TypeError: object is not a function

因为:

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


Nat*_*son 8

代码中有两个问题.

1 - 代码末尾的括号.

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

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