【问题标题】:Can not rebind scroll event?不能重新绑定滚动事件?
【发布时间】:2012-06-20 14:40:27
【问题描述】:

我想在 ajax 调用中重新绑定窗口滚动事件

$(window).scroll(function(){
   column_height = $("#first_column").height();
   screenTop = $(window).scrollTop();
   window_height = $(window).height();

if((screenTop+window_height)>=column_height){

    $(window).unbind('scroll');


      $.ajax({
            url: "/service/article_json.php",
            type: 'GET',
            async: false,
            cache: false,
            timeout: 30000,
            error: function(){
                return true;
            },
            success: function(data){ 
                $.each($.parseJSON(data), function(key,item) {

                  //Add content to #first_column

                  $(window).bind('scroll');

                });
            }
        });
  }
});

$(window).bind('scroll'); 似乎不起作用。

【问题讨论】:

    标签: javascript jquery events bind dynamic-rebinding


    【解决方案1】:

    当您.unbind 时,对绑定事件的引用不会存储在任何地方或任何地方。 $(window).bind('scroll') 实际上什么都不做。

    首先,如果你使用1.7,你应该使用.on.off。没什么大不了的。

    有几种方法可以做到这一点,但最简单的方法是单独定义函数并使用它的名称来绑定/取消绑定。您甚至可以将其附加到窗口(尽管我认为只需使用 function 关键字就可以做到这一点。无论如何..)

    $(window).data('scrollEvent', function() { /* your func here */ });
    $(window).on('scroll', $(window).data('scrollEvent'));
    

    您可以使用第二行替换上面的$(window).bind('scroll')。小心递归。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2017-10-01
      • 2013-05-06
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      相关资源
      最近更新 更多