滚动ul
时,滚动事件未触发.我使用的是jQuery 1.10.2版.当我从ajax页面加载ul
时,我无法使用$('ulId').on('scroll', function() {});
或其他实时方法.请帮我找到解决办法.
$(document).on( 'scroll', '#ulId', function(){
console.log('Event Fired');
});
滚动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
$(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');
});