jQuery timeago用法

Lon*_*don 7 javascript jquery timeago

<abbr>我的HTML中有一个带有"timeago"类的标签.当我在页面加载时设置它的值,然后调用jQuery("abbr.timeago").timeago();文档就绪功能它的工作原理.

我的问题是,如果我从某些javascript函数动态更改abbr.timeago标题怎么办,如何让timeago插件在更新的abrr.timeago元素上做出魔力呢?

我应该打电话给哪个功能?我应该jQuery("abbr.timeago").timeago();从文件准备功能中删除还是离开?谢谢

编辑问题:

@squiddy示例工作,只更新一次时间,它只是保持不变.例如,如果我将timeago放到当前时间......那么它只是表示它不会改变?

BOUNTY UPDATE:

请忽略以前的问题.在我的网站上有一个链接,点击它的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
    });
Run Code Online (Sandbox Code Playgroud)

因此,我从服务器获得的每个反馈都会调用此getJSON(n次).当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"));
            });  
        });
Run Code Online (Sandbox Code Playgroud)

我花了几个小时尝试了几个可能的解决方案..就像每隔n秒调用一个函数,调用timeago函数但总是相同的结果

aor*_*sik 13

更新2:同样的技巧在这里工作.该元素column之前曾被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
});
Run Code Online (Sandbox Code Playgroud)

更新:更正,测试,诀窍是,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");
Run Code Online (Sandbox Code Playgroud)

希望这是你需要的!