Javascript jQuery timeago用法
我的HTML中有一个带有类“timeago”的Javascript jQuery timeago用法,javascript,jquery,timeago,Javascript,Jquery,Timeago,我的HTML中有一个带有类“timeago”的标记。当我在页面加载上设置它的值时,然后调用jQuery(“abbr.timeago”).timeago()在文档准备功能上,它可以工作 我的问题是,如果我从某个javascript函数中动态更改abbr.timeago标题,我如何使timeago插件在更新的abr.timeago元素上发挥其魔力 我应该调用哪个函数?我应该删除jQuery(“abbr.timeago”).timeago()吗来自文档准备功能还是离开它?多谢各位 编辑问题: @squ
标记。当我在页面加载上设置它的值时,然后调用jQuery(“abbr.timeago”).timeago()代码>在文档准备功能上,它可以工作
我的问题是,如果我从某个javascript函数中动态更改abbr.timeago标题,我如何使timeago插件在更新的abr.timeago元素上发挥其魔力
我应该调用哪个函数?我应该删除jQuery(“abbr.timeago”).timeago()吗代码>来自文档准备功能还是离开它?多谢各位
编辑问题:
@squiddy示例有效,只更新一次时间,并且保持不变。例如,如果我将timeago设置为当前时间。。然后它就这样站着它不会改变吗
赏金更新:
请忽略前面的问题。在我的网站中,有一个链接,它的click-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
});
因此,对于我从服务器获得的每个反馈,都会调用这个getJSON(n次)。当json完成时,表中相应的列将填充上次更新的时间
但似乎timeago没有读取更改的标题值,它不识别更新的DOM。为了调试的目的,我为来自服务器的每个反馈放置了一个console.log(element.attr(“title”)),只是为了查看我是否将title属性设置为current time,确实如此,但timeago会选择最初加载的title值
我应该怎么做我也尝试过这个版本:
$.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函数,但总是得到相同的结果。更新2:相同的技巧在这里有效。元素列
以前曾被timeago转换过一次,当转换发生时,timeago在元素上放置了一个“timeago”数据字段。当此数据字段出现时,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.data("timeago",null).timeago();
// ... iso8601 is date format function
});
jQuery.timeago(new Date()); //=> "less than a minute ago"
jQuery.timeago("2008-07-17"); //=> "2 years ago"
jQuery.timeago(jQuery("abbr#some_id")); //=> "2 years ago" [title="2008-07-20"]
更新:纠正,测试,诀窍是,timeago插件标记已经转动的元素,不要让它们再次转动,除非标记被清除
更改abbr.timeago中的值时,应仅在该元素上再次运行timeago()函数。我建议使用以下功能:
$.fn.changeTimeago = function(isotime) {
return $(this).attr("title",isotime).data("timeago",null).timeago();
}
// usage:
$("abbr.timeago").changeTimeago("2004-07-17T09:24:17Z");
希望这是你需要的 在更改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.data("timeago",null).timeago();
// ... iso8601 is date format function
});
jQuery.timeago(new Date()); //=> "less than a minute ago"
jQuery.timeago("2008-07-17"); //=> "2 years ago"
jQuery.timeago(jQuery("abbr#some_id")); //=> "2 years ago" [title="2008-07-20"]
因此,在更改元素时,请自己更新html:
jQuery("abbr.timeago").html(jQuery.timeago("2010-07-17T09:24:17Z"));
除此之外,不要添加任何javascript或jquery代码
$('.timeago').timeago();
然后添加“Z”(或包括T)。有关更多信息,请访问此
从v1.1.0开始,jquery.timeago.js现在提供了一个更新
操作,可用于:
发件人:
如果希望使用新日期更新timeago元素。最简单的方法如下-
$('abbr.timeago').timeago('update',(new Date()).toISOString());
它就像一个符咒。我不明白为什么它没有记录在任何地方。您可以通过查看timeago javascript代码找到此函数。@London是的,您是对的,但我已更正了它。顺便说一句,timago()适用于标题中的任何时间字符串,Date()对象可以解析该字符串,例如2011年2月4日16:16:00您的示例适用,只更新一次时间,并且保持不变。例如,如果我将timeago设置为当前时间。。然后它就这样站着它不会改变吗?我是否应该将jQuery(“abbr.timeago”).timeago()留在我的document ready函数中?或者定期给它打电话?如果你问:是的,它也会自动更新内容。