最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Attaching Event Handlers after Infinite Scroll Update - Stack Overflow

programmeradmin1浏览0评论

I am using the infinite scroll plugin / to load page content.

I have a jQuery event listener such as:

$('.like-action').on('click',
    function(event){
      likeComment($(this)); 
      event.preventDefault();
});

That get's loaded on $(document).ready. However, when new content is loaded with the infinitescroll the event listeners are not applied/available to the new content. So, I created a function that is called on the callback of the infinitescroll (when all content has loaded).The function:

function afterUpdate(){
$('.like-action').on('click',function(event){
      likeComment($(this)); 
      event.preventDefault();
});}

What ends up happening however, is for the old content (that is already loaded) when a link is clicked that has the .like-action class, the likeComment function is called however many times new content has been loaded + the original $(document).ready.

Ex: content is loaded on page load. link executes likeComment 1 on click. After scrolling down and having new content loaded (and callback) if you click the same link as before, likeComment is executed twice. etc,etc.

What am I doing wrong here?

Is my event listener written incorrectly?

Is there a way to write a listener that automatically works for all elements in the DOM even if they were not there on page load?

Or, is there a way to only register the .on on elements that were just loaded by infinite scroll (so there isn't a duplication)?

I am using the infinite scroll plugin http://www.infinite-scroll./infinite-scroll-jquery-plugin/ to load page content.

I have a jQuery event listener such as:

$('.like-action').on('click',
    function(event){
      likeComment($(this)); 
      event.preventDefault();
});

That get's loaded on $(document).ready. However, when new content is loaded with the infinitescroll the event listeners are not applied/available to the new content. So, I created a function that is called on the callback of the infinitescroll (when all content has loaded).The function:

function afterUpdate(){
$('.like-action').on('click',function(event){
      likeComment($(this)); 
      event.preventDefault();
});}

What ends up happening however, is for the old content (that is already loaded) when a link is clicked that has the .like-action class, the likeComment function is called however many times new content has been loaded + the original $(document).ready.

Ex: content is loaded on page load. link executes likeComment 1 on click. After scrolling down and having new content loaded (and callback) if you click the same link as before, likeComment is executed twice. etc,etc.

What am I doing wrong here?

Is my event listener written incorrectly?

Is there a way to write a listener that automatically works for all elements in the DOM even if they were not there on page load?

Or, is there a way to only register the .on on elements that were just loaded by infinite scroll (so there isn't a duplication)?

Share Improve this question edited Oct 10, 2012 at 12:46 Marc 16.5k20 gold badges79 silver badges121 bronze badges asked Oct 10, 2012 at 12:43 w00tw00t111w00tw00t111 3595 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Change your usage of on() to pass a selector and it will use event delegation, causing the click handlers to work for all future elements as well:

$('#someContainer').on('click', '.like-action', function (event){
    likeComment($(this)); 
    event.preventDefault();
});

This will prevent your click handlers from ever needing to be added again, solving your issue of them being added multiple times to older elements.

This assumes that all of your current and future .like-action elements will be contained inside of #someContainer.

Edit: in response to your ment, you cannot delegate plugin initialization like that. what you could do is this: when you initialize an element, add a class to it as well:

$('.profilecard').hovercard().addClass('initialized');

Then in your callback when you need to initialize the new ones, skip over anything that has that class already:

function afterUpdate(){ 
    $('.profilecard:not(".initialized")').hovercard();
}

If anyone is interested in doing this without jQuery, there is a great post here - https://elliotekj./2016/11/05/jquery-to-pure-js-event-listeners-on-dynamically-created-elements/

Basic example:

document.querySelector(staticParent).addEventListener(eventName, function (event) {
  if (event.target.classList.contains(dynamicChildSelector)) {
    // Do something
  }
})

Working example:

document.querySelector('.feed').addEventListener('click', function (event) {
  if (event.target.classList.contains('feed-item')) {
    // Do something
  }
})
发布评论

评论列表(0)

  1. 暂无评论