【问题标题】:How can I check if scroll binding was added to window?如何检查滚动绑定是否已添加到窗口?
【发布时间】:2014-03-16 11:26:52
【问题描述】:
我想检查滚动事件是否绑定到 jquery 中的窗口元素。基本上我想看看是否有人做过这样的事情:
$(window).scroll(function() { ... });
在搜索了一些关于 SO 的信息后,我发现了更常见的问题,例如 if event already exist on an element 以及一个可行的解决方案。
但是当我尝试使用$.data( $(window).get(0), 'events' ) 时,我得到了undefined。 here 也提出了类似的解决方案。
那么检查滚动事件的正确方法是什么?
【问题讨论】:
标签:
javascript
jquery
javascript-events
【解决方案1】:
我想这就是你想要的:
$._data(window).events.scroll
如果没有绑定到window对象的此类事件,则返回undefined
例子:
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