1

我正在使用 Jquery 插件http://timeago.yarp.com/来显示时间。

问题是 timeago 不会对动态生成的项目生效。

    $(document).ready(function() {

         $(".timeago").timeago();       // works perfectly fine for the items which are loaded on page load

         //$(".timeago").live(timeago());      // gives me an error ie timeago is not defined

         //$(".timeago").live($(".timeago").timeago());  // gives me an error too much recursion.
         jQuery.timeago.settings.allowFuture = true;
});

通过一些谷歌搜索,我知道了一些事情,即:

使用 live 与使用 bind 相同,只是它仅限于事件 click、dblclick、keydown、keypress、keyup、mousedown、mousemove、mouseout、mouseover 和 mouseup。

现在怎么办,因为我没有任何点击事件?我该如何绑定这个?

4

2 回答 2

0

看看这个主题,这里讨论了如何将 timeago 放在动态加载的项目上,例如 ajax 请求的结果。

仅在新添加的元素上激活 timeago

PS:allowFuture 与将 timeago 放在页面上新创建的项目上没有任何关系。它只允许未来的日期(fe “3 天内”,“下周”)

于 2011-09-19T13:40:58.630 回答
0

.live()并将.bind()回调分配给事件。在您的情况下,您没有将功能分配给的事件,因此它失败了。

理论上,您可以将回调分配给自定义事件。.trigger()但是,无论何时生成项目,您都必须手动触发事件(使用)。例如:

$("abbr.timeago").live("timeago", function() {
   $(this).timeago();
});

// ... and in the bit that generates your item
$new_item.trigger("timeago")

演示:http: //jsfiddle.net/ZjuW4/9

当然,.live()在这种情况下使用纯粹是学术性的,并没有真正起到很好的作用

如果您确实有权访问生成项目的代码,则可以简单地在生成项目时链接到调用.timeago(),即http://jsfiddle.net/ZjuW4/3/

于 2011-09-19T14:10:10.953 回答