jQuery 类型错误:$(...).on 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15670352/
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-26 15:33:29  来源:igfitidea点击:

TypeError: $(...).on is not a function

jquerycakephp-1.3lightbox

提问by user1613870

I am using jQuery litebox. After adding JS and CSS files I got this error TypeError:

我正在使用jQuery litebox。添加 JS 和 CSS 文件后,我收到此错误 TypeError:

$(...).on is not a function at this line in js file                                           
 "return  $('body').on('click',       
'a[rel^=lightbox], area[rel^=lightbox]', function(e) {" 

Can anybody help me to understand the problem here?

有人可以帮我理解这里的问题吗?

I am doing this implementation in CakePHP 1.3.

我在 CakePHP 1.3 中做这个实现。

回答by Shivang Gupta

The problem may be if you are using older version of jQuery. Because older versions of jQuery have 'live' method instead of 'on'

如果您使用的是旧版本的 jQuery,则可能会出现问题。因为旧版本的 jQuery 有 'live' 方法而不是 'on'

回答by T.J. Crowder

The usual cause of this is that you're alsousing Prototype, MooTools, or some other library that makes use of the $symbol, and you're including that library after jQuery, and so that library is "winning" (taking $for itself). So the return value of $isn't a jQuery instance, and so it doesn't have jQuery methods on it (like on).

造成这种情况的通常原因是您使用了 Prototype、MooTools 或其他一些使用该$符号的库,并且您在 jQuery 之后包含了该库,因此该库是“获胜”($为自己考虑) . 所以 的返回值$不是一个 jQuery 实例,所以它上面没有 jQuery 方法(比如on)。

You can use jQuery with those other libraries, but if you do, you have to use the jQuerysymbol rather than its alias $, e.g.:

您可以将 jQuery 与其他库一起使用,但如果这样做,则必须使用jQuery符号而不是其别名$,例如:

jQuery('body').on(...);

And it's usually best if you add this immediately after your scripttag including jQuery, before the one including the other library:

通常最好在script包含 jQuery的标签之后立即添加它,在包含其他库的标签之前:

<script>jQuery.noConflict();</script>

...although it's not requiredif you load the other library after jQuery (it is if you load the other library first).

...虽然如果您在 jQuery 之后加载另一个库则不需要(如果您先加载另一个库)。

Using multiple full-function DOM manipulation libraries on the same page isn't ideal, though, just in terms of page weight. So if you can stick with just Prototype/MooTools/whatever or just jQuery, that's usually better.

但是,就页面权重而言,在同一页面上使用多个全功能 DOM 操作库并不理想。因此,如果您可以坚持使用 Prototype/MooTools/whatever 或仅使用 jQuery,那通常会更好。

回答by Oskar

This problem is solved, in my case, by encapsulating my jQuery in:

在我的情况下,通过将我的 jQuery 封装在:

(function($) {
//my jquery 
})(jQuery);

回答by Adhish

If you are using old version of jQuery(< 1.7) then you can use "bind" instead of "on". This will only work in case you are using old version, since as of jQuery 3.0, "bind" has been deprecated.

如果您使用的是旧版本的 jQuery(< 1.7),那么您可以使用“bind”而不是“on”。这仅在您使用旧版本的情况下才有效,因为从 jQuery 3.0 开始,“绑定”已被弃用。

回答by ownking

I tried the solution of Oskar (and many others) but for me it finaly only worked with:

我尝试了 Oskar(和许多其他人)的解决方案,但对我来说它最终只适用于:

jQuery(function($){
   // Your jQuery code here, using the $
});

See: https://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

请参阅:https: //learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/

回答by Mohammad Sadegh Maleki

In my case this code solved my error :

在我的情况下,这段代码解决了我的错误:

(function (window, document, $) {
            'use strict';
             var $html = $('html');
             $('input[name="myiCheck"]').on('ifClicked', function (event) {
             alert("You clicked " + this.value);
             });
})(window, document, jQuery);

You don't should put your function inside $(document).ready

你不应该把你的函数放在 $(document).ready 里面