简体   繁体   English

append 项目后的滚动事件不起作用

[英]scroll event after append item Not working

Trying to call scroll event after append item but it is not working.试图在 append 项目之后调用滚动事件,但它不起作用。 what I do wrong?我做错了什么? Please help me Thank you.请帮帮我谢谢。

you can check the code here https://jsfiddle.net/4uk92cxe/1/你可以在这里查看代码https://jsfiddle.net/4uk92cxe/1/

 $(document).ready(function () { $('#myDiv2').append( "<table class='scroll-item'><tr><td> YYYYYYYYYYYYYY</td></tr></table>" ); $('.scroll-item').on('scroll', function () { console.log('scrolling'); }); $(document).on('scroll', "table[class='scroll-item']", function () { console.log('scrolling'); }); $('.scroll-item').scroll(function () { console.log('scrolling'); }); $(document).on('scroll', '.scroll-item', function () { console.log('scrolling'); }); });
 .wrapper { width:100px; overflow-x: scroll; overflow-y: hidden; } table { width: 300px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <div class="wrapper"> <table class="scroll-item"> <tr> <td> XXXXXXXXXXXXXXXXXX</td> </tr> </table> </div> <div id="myDiv2" class="wrapper"></div>

Try change the class scroll-item in the table.尝试更改表中的 class 滚动项。 Rmove it from table, and put it in the div content .wrapper that is holding the scrollbar.从表格中移除它,并将其放入包含滚动条的 div 内容.wrapper中。 Example例子

 $(document).ready(function () { $("#myDiv2").addClass("scroll-item"); $('#myDiv2').append( "<table ><tr><td> YYYYYYYYYYYYYY</td></tr></table>" ); $('.scroll-item').on('scroll', function () { console.log('scrolling'); }); });
 .wrapper { width: 100px; overflow-x: scroll; overflow-y: hidden; } table { width: 300px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <div class="scroll-item wrapper"> <table> <tr> <td> XXXXXXXXXXXXXXXXXX</td> </tr> </table> </div> <div id="myDiv2" class="wrapper"></div>

Just add the class in the div: $("#myDiv2").addClass("scroll-item");只需在 div 中添加 class : $("#myDiv2").addClass("scroll-item"); and also, replace this class here <div class="scroll-item wrapper"> .并且,在此处替换此 class <div class="scroll-item wrapper">

You can also remove this: $("#myDiv2").addClass("scroll-item");您也可以删除它: $("#myDiv2").addClass("scroll-item"); and add the class directly in the HTML to avoid more work with Jquery.并直接在 HTML 中添加 class 以避免与 Jquery 进行更多工作。

The answer to your question what I do wrong?你的问题的答案我做错了什么? is, you are calling on scroll on .scroll-item but the scroller is in the .wrapper .是,您正在调用.scroll-item上的滚动,但滚动条位于.wrapper中。

use this instead,改用这个,

$('.wrapper').on('scroll', function(){
  console.log('scrolling');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM