Uncaught TypeError: Object [object Object] has no

2019-01-11 06:32发布

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.

Here is my script that doesn't work with older versions of 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.

4条回答
迷人小祖宗
2楼-- · 2019-01-11 06:50

You could use delegate(), for example:

$("table").delegate("td", "click", function() {
  $(this).toggleClass("chosen");
});

This is equivalent to the following code written using on():

$("table").on("click", "td", function() {
  $(this).toggleClass("chosen");
});
查看更多
Evening l夕情丶
3楼-- · 2019-01-11 06:52

Change the on function to bind:

.children().on("click",function()
to
.children().bind("click",function()

查看更多
forever°为你锁心
4楼-- · 2019-01-11 06:54

You must use bind instead of on, as on was only introduced in 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);           
    })
})
查看更多
孤傲高冷的网名
5楼-- · 2019-01-11 06:58

You must use jQuery version 1.7+ to use on(). bind() is deprecated.

查看更多
登录 后发表回答