滚动ul时,滚动事件未触发.我使用的是jQuery 1.10.2版.当我从ajax页面加载ul时,我无法使用$('ulId').on('scroll', function() {});或其他实时方法.请帮我找到解决办法.

$(document).on( 'scroll', '#ulId', function(){
    console.log('Event Fired');
});

推荐答案

You probably forgot to give # before id for id selector, you need to give # before id ie is ulId

You probably need to bind the scroll event on the div that contains the ul and scrolls. You need to bind the event with div instead of `ul`
$(document).on( 'scroll', '#idOfDivThatContainsULandScroll', function(){
    console.log('Event Fired');
});

Edit

The above would not work because the scroll event does not bubble up in DOM which is used for event delegation, see this question why doesn't delegate work for scrolling.

但使用现代浏览器>;你可以用另一种方式.与使用jquery进行委派不同,您可以使用javascript document.addEventListener的事件捕获来完成委派,第三个参数为true;在这tuturial个例子中,看看冒泡和捕捉是如何工作的.

100

document.addEventListener('scroll', function (event) {
    if (event.target.id === 'idOfUl') { // or any other filtering condition        
        console.log('scrolling', event.target);
    }
}, true /*Capture event*/);

如果您不需要事件委派,那么您可以直接将scroll事件绑定到ul,而不是通过document委派它.

100

$("#idOfUl").on( 'scroll', function(){
   console.log('Event Fired');
});

Jquery相关问答推荐

使用 JQuery 在 span 标签中用逗号分隔页面上的文本

将元素附加到每个函数 jQuery

jquery克隆div并将其附加在特定div之后

为什么 jQuery 不使用 requestAnimationFrame?

在 jQuery 事件中控制this的值

jQuery javascript 正则表达式 将
替换为 \n

如何判断是否有任何 JavaScript 事件侦听器/处理程序附加到元素/文档?

这些 jQuery 就绪函数之间有什么区别?

使用 jQuery click 处理锚点 onClick()

无法在被动事件侦听器中阻止默认值

jQuery/JavaScript 碰撞检测

删除所有子元素的 CLASS

确定 JavaScript 值是否为整数?

如何知道字体(@font-face)是否已经加载?

jquery获取帖子操作网址

使html文本输入字段随着我输入而增长?

jQuery:如何从 $.ajax.error 方法中获取 HTTP 状态代码?

判断文本框是否有空值

使用 jQuery 在 Select 列表中隐藏选项

'touchstart' 事件是否有与点击事件相同的 e.PageX 位置?