Javascript 无法重新绑定滚动事件?
我想在ajax调用中重新绑定窗口滚动事件Javascript 无法重新绑定滚动事件?,javascript,jquery,events,bind,dynamic-rebinding,Javascript,Jquery,Events,Bind,Dynamic Rebinding,我想在ajax调用中重新绑定窗口滚动事件 $(window).scroll(function(){ column_height = $("#first_column").height(); screenTop = $(window).scrollTop(); window_height = $(window).height(); if((screenTop+window_height)>=column_height){ $(window).unbind('sc
$(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')代码>似乎不起作用 当您.unbind
时,对绑定事件的引用不会存储在任何地方或任何东西<代码>$(窗口).bind('scroll')
实际上什么都不做
首先,如果您使用的是1.7
,则应改为使用.on
和.off
。没什么大不了的
有几种方法可以做到这一点,但最简单的方法是单独定义函数并使用其名称绑定/取消绑定。您甚至可以将其附加到窗口(尽管我认为只需使用函数
关键字就可以了。无论如何
您可以使用第二行替换上面的$(窗口).bind('scroll')
。小心递归
$(window).data('scrollEvent', function() { /* your func here */ });
$(window).on('scroll', $(window).data('scrollEvent'));