【问题标题】:Can't delegate a scroll event using "on"无法使用“on”委托滚动事件
【发布时间】:2015-06-05 22:41:49
【问题描述】:

我试图委托一个滚动事件,以便我的元素在 ajax 调用返回时仍然具有相同的处理程序。 我使用on 执行我的委托

$('body').on({
    scroll:function(){
        alert('scrolling');
    },
    click:function(){
        alert('clicked');
    }
},'.pt_pr_3_2');

点击事件有效,但滚动无效。 Here is the explanation why the scroll doesn't work

我想知道我是否还有其他选择?

【问题讨论】:

    标签: javascript jquery delegates


    【解决方案1】:

    你可以试试这个:

    $('.pt_pr_3_2')
      .on('scroll', function(){
            alert('scrolling');
        })
      .on('click', function(){
            alert('clicked');
        });
    

    【讨论】:

    • 但是当元素从 ajax 调用返回时它不会工作,对吧?
    • @KesongXie 为什么不呢。这里您使用的是on,因此它对动态创建的元素有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多