【发布时间】:2011-02-04 14:42:13
【问题描述】:
我的 HTML 中有一个 <abbr> 标记,其类为“timeago”。当我在页面加载时设置它的值时,然后在文档就绪功能上调用jQuery("abbr.timeago").timeago(); 它可以工作。
我的问题是,如果我从某个 javascript 函数中动态更改 abbr.timeago 标题,我怎样才能让 timeago 插件在更新后的 abrr.timeago 元素上发挥它的魔力?
我应该调用哪个函数?我应该从文档准备功能中删除jQuery("abbr.timeago").timeago(); 还是保留它?谢谢
编辑问题:
@squiddy 示例有效,仅更新一次时间,并且保持不变。例如,如果我将 timeago 设置为 current time .. 那么它就像它不会改变一样?
赏金更新:
请忽略之前的问题。在我的网站中,点击 getJSON 函数时有一个链接,该函数将一些信息从服务器获取到表中。
每一行代表来自服务器的反馈。这是我的 getJSON 伪代码:
$.getJSON("feedback/" + feedbackId, function(data) {
var column ... declared variable in which column selector
.... some logic that populates table(irrelevant)
//Store last checked time in title
column.attr("title", iso8601(new Date()));
//invoke timeago on this title
column.html(jQuery.timeago(iso8601(new Date())));
....iso8601 is date format function
});
因此,对于我从服务器获得的每个反馈(n 次),都会调用这个 getJSON。当 json 完成时,表中的相应列将填充最后更新时间。
但似乎 timeago 没有读取更改的标题值,它无法识别更新的 DOM。我为来自服务器的每个反馈放置了一个 console.log(element.attr("title")) 用于调试目的,只是为了查看我是否将 title 属性设置为当前时间,确实如此,但 timeago 会选择最初加载的标题价值。
怎么办我也试过这个版本:
$.getJSON("feedback/" + feedbackId, function(data) {
var column ... declared variable in which column selector
.... some logic that populates table(irrelevant)
//Store last checked time in title
column.attr("title", iso8601(new Date()));
//invoke timeago on this title
$(column).livequery(function() {
$(this).timeago(iso8601(new Date()));
console.log($(this).attr("title"));
});
});
我花了几个小时尝试了几种可能的解决方案 .. 比如每隔 n 秒调用一次的函数,调用 timeago 函数,但结果总是相同
【问题讨论】:
标签: javascript jquery timeago