简体   繁体   English

如何将 jQuery 事件应用于使用 Ajax get 请求动态加载的内容

[英]How to apply jQuery events to a content that is dynamically loaded using an Ajax get request

This is the jQuery I want the put in the content I am requesting when the page load:这是我想要在页面加载时放入我请求的内容的 jQuery:

 $(".allComments").hide(); $(".viewAll").click(function(e) { e.stopImmediatePropagation() if ($(window).scrollTop() >= 150) { $([document.documentElement, document.body]).animate({ scrollTop: $(this).offset().top }, 1000); } console.log("view all commment is click") $(this).parent().siblings('div.allComments').slideToggle(); })

So when the content loads, I want to hide the comment section and the user will be able to press a button to reveal the comments..所以当内容加载时,我想隐藏评论部分,用户将能够按下一个按钮来显示评论..

the problem is the jQuery is not taking into effects.... I know we can use the jQuery on() event but I do not know to use it, if I want the hide the comments when the page loads..问题是 jQuery 没有考虑到效果......我知道我们可以使用jQuery on() 事件,但我不知道使用它,如果我想在页面加载时隐藏评论..

Any ideas how to achieve that?任何想法如何实现这一目标?

 // Load dynamic content $(document).ready(function(){ var comments = ''; for(var i = 0 ; i < 10; i++){ comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>'; } $('#comment-section').append(comments).hide(); }); // View all button click $(document).on('click','#view-all',function(){ $('#comment-section').show(); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="comment-section"></div> <button id="view-all">View All</button>

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

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