2010-11-10 21 views
0

我有一個調整版本的jQuery TimeAgo插件..唯一的調整是,我可以設置自定義屬性,而不是全局的。這是因爲我可以在某些標籤上有後綴,而在其他標籤上沒有後綴。由於我已經以不同的方式對其進行了測試,因此這一調整完全可以自行完成。jQuery - 我怎麼做「每元素屬性評估」

但是,我想選擇元素/屬性的方式工作不正常。

我的標記有以下

<time data-suffixAgo="" datetime="2010-06-24T14:10:22Z" title="2010-06-24T14:10:22Z">4 months </time> 
<time data-suffixAgo="ago" datetime="2010-11-10T06:00:44Z" title="2010-11-10T06:00:44Z">less than a minute ago</time> 
<!-- note the data between the <title> tags is generated server side. 
I'm not talking about that data, just the jQuery information--> 

並在運行時輸出表示

4 months ago 
less than a minute ago 

現在有了這個問題是,「4個月前」不應該說「前」因爲我的data-suffixAgo屬性爲空

這裏是我正在使用的jQuery

// selects all "time" tags on the page and give the friendly "timeago" 
// uses the jquery.timeago.js plugin 
// all time tags are created using the "ToTimeSpan" Date Extension Method 
$('time').timeago({strings:{suffixAgo: $(this).attr("data-suffixAgo")}}); 

任何人都可以告訴我爲什麼我的選擇器不是基於每個元素拉動data-suffixAgo屬性?

如果我用這個

$('time').timeago({strings:{suffixAgo: ""}}); 

的 「前」 將是刪除所有<time>標籤,如果我這樣做

$('time').timeago({strings:{suffixAgo: "ago"}}); 

的 「前」 將是添加 to all <time>標籤

回答

2

Try:

$('time').each(function(){ 
    $(this).timeago({ 
     strings: {suffixAgo: $(this).attr("data-suffixAgo") } 
    }); 
}); 

您在代碼中的$(this)引用了整個$('time')集合。

+0

你是光榮的!謝謝。現在我學到了一些新東西。 – 2010-11-10 06:22:03