如何根据时间将标记添加到时间戳td

时间:2014-02-24 18:08:26

标签: jquery

我想根据新闻的生成时间为td.timestamp类添加标记。

我没有能力添加到现有的HTML中,所以必须使用jQuery,我的经验几乎为零。

我想做的是:

如果td.timestamp有"分钟,分钟,小时,小时或1天"然后插入标签" new"

如果td.timestamp有"天"然后插入标签"最近"

所以html现在看起来像这个

<td class="timestamp new">10 minutes</td>
<td class="timestamp new">5 hours</td>
<td class="timestamp recent">2 days</td>

这是现有的html是如何

<table class="report" align="center" cellspacing="1">
<caption><span>Latest Player News</span></caption>
<tbody>

<tr>
<th class="headline">Headline</th>
<th class="timestamp">Published</th>
</tr>

<tr class="oddtablerow">
<td class="headline">
<a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=2014-02-21T21%3A47%3A00Rotoworld3291">Peyton Manning to have physical next wee...</a>
</td>
<td class="timestamp">10 minutes</td>
</tr>

<tr class="eventablerow">
<td class="headline">
<a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=893058KFFL">Broncos | Peyton Manning will not need to restructure deal</a>
</td>
<td class="timestamp">1 hour</td>
</tr>

<tr class="eventablerow">
<td class="headline">
<a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=893036KFFL">Broncos | Peyton Manning to be evaluated</a></td>
<td class="timestamp">5 hours</td>
</tr>

<tr class="oddtablerow">
<td class="headline">
<a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=893036KFFL">Broncos | Peyton Manning to be evaluated</a></td>
<td class="timestamp">1 day</td>
</tr>

<tr class="eventablerow">
<td class="headline"><a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=892708KFFL">Jaguars | Will not force picking a quarterback</a></td>
<td class="timestamp">4 days</td>
</tr>

<tr class="oddtablerow">
<td class="headline">
<a href="http://football25.myfantasyleague.com/2014/view_news_article?L=77815&amp;ID=893036KFFL">Broncos | Peyton Manning to be evaluated</a></td>
<td class="timestamp">20 days</td>
</tr>

</table>

1 个答案:

答案 0 :(得分:2)

这个jQuery示例将查找字符串“days”,如果找到该字符串,则插入“recent”。任何其他类型将获得“新”类。 See this fiddle.

以下是代码:

$(function() {
  $("td.timestamp").each(function( index ) {

    var timeStampType = $(this).text();

    if (timeStampType.toLowerCase().indexOf("days") >= 0)
    {
        $(this).addClass("recent");
    }
    else
    {
        $(this).addClass("new");
    }
  });

});