jQuery:获取.timeago()来处理DOM就绪后加载的元素(Ajax)

时间:2022-05-17 10:14:47

Using pageless1 to trigger an ajax load of older, timestamped content onto the end of the page, I need .timeago() to act upon those newly loaded dates. It works on the content loaded the first time you arrive at the page, but not the content loaded via Ajax later.

使用pageless1来触发一个ajax加载,在页面末尾加上时间戳的旧内容,我需要.timeago()来处理这些新加载的日期。它作用于第一次到达页面时加载的内容,而不是稍后通过Ajax加载的内容。

Thanks!

谢谢!

1 个解决方案

#1


9  

For running plugins (things not event handler based) you can still use the .livequery() plugin, like this:

对于运行插件(不是基于事件处理程序的),您仍然可以使用.livequery()插件,如下所示:

$('.selector').livequery(function() {
  $(this).timeago();
});

If it's an event handler, .live() or .delegate() works, or if it's a plugin you can also run the plugins on loaded content, for example:

如果它是一个事件处理程序,.live()或.delegate()工作,或者如果它是一个插件,你也可以在加载的内容上运行插件,例如:

$.ajax({
  //options...
  success: function(data) {
    //do stuff
    $('.selector', data).timeago();
  }
});

This runs the plugin on that selector, but only for elements that came back in that AJAX response, by using the context argument of $(selector, [context]).

它在那个选择器上运行插件,但是只针对在AJAX响应中返回的元素,使用$(选择器,[context])的上下文参数。

#1


9  

For running plugins (things not event handler based) you can still use the .livequery() plugin, like this:

对于运行插件(不是基于事件处理程序的),您仍然可以使用.livequery()插件,如下所示:

$('.selector').livequery(function() {
  $(this).timeago();
});

If it's an event handler, .live() or .delegate() works, or if it's a plugin you can also run the plugins on loaded content, for example:

如果它是一个事件处理程序,.live()或.delegate()工作,或者如果它是一个插件,你也可以在加载的内容上运行插件,例如:

$.ajax({
  //options...
  success: function(data) {
    //do stuff
    $('.selector', data).timeago();
  }
});

This runs the plugin on that selector, but only for elements that came back in that AJAX response, by using the context argument of $(selector, [context]).

它在那个选择器上运行插件,但是只针对在AJAX响应中返回的元素,使用$(选择器,[context])的上下文参数。