未捕获的TypeError:对象[object Object]没有方法'on'

dvl*_*den 28 jquery bind onclick

任何人都可以帮我解决这个问题吗?

当我使用jQuery的最新版本(或新版本)时,下面的小脚本工作正常.但是,当我使用旧版本的jQuery时,我的脚本说该on函数不存在.

这是我的脚本,不适用于旧版本的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);           
    })
})
Run Code Online (Sandbox Code Playgroud)

任何形式的帮助表示赞赏.

Ael*_*ios 75

你必须使用bind而不是onjQuery 1.7中on引入的那样.

$(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);           
    })
})
Run Code Online (Sandbox Code Playgroud)

  • @ F.Calderan答案并没有解决*为什么*,尽管这个问题从未明确问过为什么.它只是说"这是修复",没有解释原始问题是什么; 我会让人们自己决定这是否构成一个完整的答案. (7认同)
  • @Alex - 在这种情况下,`bind`和`on`函数以相同的方式运行.没有选择器被传递给`on`,这将导致它委托事件处理程序. (4认同)