滚动事件监听器未触发
Scroll event listener not firing
我无法让事件侦听器在滚动时触发。它可以在点击时工作,但不能滚动。
我希望它的工作,即使它正在监听的div是创建之后的侦听器本身,因为ajax的用法。所以我用.on
代替.scroll
。
$('.container').on('scroll', '.appended', function(){
console.log('scrolling!');
});
我在这里创建了一个JSFiddle
您需要将侦听器附加到追加元素。像这样:
var nAppend = $('<div class="appended">this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br></div>').scroll(function(){
console.log('scrolling!');
});
setTimeout(function(){$('.container').append(nAppend);},1000);
我更新了你的文件。
滚动事件不会冒泡,因此委托方法不起作用。
使用mousewheel
事件监听器:
$('.container').on('mousewheel', '.appended' ,function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('scrolling up !');
}
else{
console.log('scrolling down !');
}
});
相关文章:
- 在下拉式javascript事件监听器中选择时触发事件
- 如何在音频元素上使用React媒体合成事件监听器
- Javascript事件监听器让我很困惑
- javascript的事件监听器将下拉列表转换为输入按钮
- 组合类和事件监听器:`this`关键字
- 将事件监听器添加到画布时出错
- 如何通过javascript触发事件监听器
- 通过事件监听器在circle的文本匹配id上修改circle元素
- 动态添加javascript事件监听器,做一些无法解释的事情
- 事件处理程序&事件监听器彼此独立
- 为什么我在谷歌闭包中编写的事件监听器不起作用
- 如何在JS中使用事件监听器
- 为什么jQuery选择事件监听器会多次触发
- 为什么不'我的手机上没有设备就绪的事件监听器
- 何时在 Cocos2d-js 3.2 中注销事件监听器和定时回调
- javaScript-我们如何将一个事件监听器设置为另一个事件监听器的从属
- 按键监听器事件在firefox中不起作用
- 谷歌地图监听器事件的行为类似于点击,即使它是鼠标悬停
- 事件处理程序和监听器&事件冒泡和事件捕获
- backbone.js:防止在模型改变时触发监听器事件