我想检查是否在jquery中将scroll事件绑定到window元素。基本上我想看看有人做过这样的事情:
$(window).scroll(function() { ... });
在寻找SO之后,我找到了更常见的问题,例如if event already exist on an element和一个有效的解决方案。
但是当我尝试使用$.data( $(window).get(0), 'events' )
时,我得到undefined
。提出了类似的解决方案here。
那么检查滚动事件的正确方法是什么?
答案 0 :(得分:2)
我想这就是你想要的:
$._data(window).events.scroll
如果没有此类事件,则返回undefined
绑定到window
对象
示例:
alert($._data(window).events.scroll); // Should return 'undefined'
$(window).scroll(function() {
alert('a');
});
alert($._data(window).events.scroll); // Should return 1 object
$(window).scroll(function() {
alert('b');
});
alert($._data(window).events.scroll); // Should return 2 objects