将jquery事件滚动与“;在“上”;

Binding jquery event scroll with "on"

本文关键字:jquery 事件 滚动      更新时间:2023-09-26

我有一个问题,为什么这个工作

$(selector).scroll(function(e) {
    console.log("Scrolling");
});

而这不是

$(document).on("scroll", selector, function(e) {
    console.log("Scrolling");
});

这是选择器

var selector = "#schedule td > div.k-scheduler-content, #schedule td > div.k-scheduler-header";

按"on"滚动事件不起作用。。。我不知道为什么。。。检查这个plunkerhttp://plnkr.co/edit/CjfoIPTCqScS1znAwmIs?p=preview

更新

也许我找到了一个可行的解决方案:

document.addEventListener(
  'scroll',
  function(event){
    var $elm = $(event.target);
    if( $elm.is(selector) ) { // or any other filtering condition
      // do some stuff
      console.log('scrolling');
    } else {
      console.log("selector different");
    }
  },
  true // Capture event
);

试着像下面这样使用它:

$("#test").on("scroll", function() {
    console.log("Scrolling");
});

希望这能有所帮助。