jQuery 未捕获的类型错误:对象 [object Object] 没有方法“on”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10912346/
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
Uncaught TypeError: Object [object Object] has no method 'on'
提问by dvlden
Can anyone help me to figure this out ?
谁能帮我解决这个问题?
When I use the latest (or a newish) version of jQuery, the small script below works fine. However, when I use older versions of jQuery, my script says that the on
function does not exist.
当我使用最新(或新)版本的 jQuery 时,下面的小脚本工作正常。但是,当我使用旧版本的 jQuery 时,我的脚本说该on
函数不存在。
Here is my script that doesn't work with older versions of jQuery:
这是我的脚本不适用于旧版本的 jQuery:
$(document).ready(function () {
$(".theImage").on("click", function(){
// In the event clicked, find image, fade slowly to .01 opacity
$(this).find("img").fadeTo("slow", .01).end()
// Then, of siblings, find all images and fade slowly to 100% opacity
.siblings().find("img").fadeTo("slow", 1);
})
})
Any kind of help is appreciated.
任何形式的帮助表示赞赏。
回答by Aelios
You must use bind
instead of on
, as on
was only introduced in jQuery 1.7.
您必须使用bind
代替on
,因为on
它只在jQuery 1.7 中引入。
$(document).ready(function () {
$(".theImage").bind("click", function(){
// In the event clicked, find image, fade slowly to .01 opacity
$(this).find("img").fadeTo("slow", .01).end()
// Then, of siblings, find all images and fade slowly to 100% opacity
.siblings().find("img").fadeTo("slow", 1);
})
})
回答by raycchan
You could use delegate()
, for example:
您可以使用delegate()
,例如:
$("table").delegate("td", "click", function() {
$(this).toggleClass("chosen");
});
This is equivalent to the following code written using on()
:
这等效于以下使用 编写的代码on()
:
$("table").on("click", "td", function() {
$(this).toggleClass("chosen");
});
回答by Mohammad Rahmani
You must use jQuery version 1.7+ to use on()
. bind()
is deprecated.
您必须使用 jQuery 1.7+ 版才能使用on()
. bind()
已弃用。
回答by Devendra Bhandari
Change the on
function to bind
:
将on
函数更改为bind
:
.children().on("click",function()
to.children().bind("click",function()
.children().on("click",function()
到.children().bind("click",function()