2016-10-04 75 views
0

有多個錶行。類「日期」的表格單元格可以包含日期或值「永久」。如果有一個日期,並且這個日期大於今天的日期,則應該用紅色標出。獲取日期從​​如果有一個

我試圖(但不工作):

if(($(".dates").getDate() < new Date()) != -1) { 
     $(".dates").style.color = "green"; 
} 

HTML:

<table> 
    <tr> 
    <td>Permanent</td> 
    </tr> 
    <tr> 
    <td>Permanent</td> 
    </tr> 
    <tr> 
    <td>2.05.2015</td> 
    </tr> 
</table> 
+0

顯示其值 –

+0

$( 「日期」)的樣本HTML。GETDATE()我不認爲這是正確的 –

+0

請出示您更多的代碼?你的日期格式是什麼? –

回答

0

$(".dates").each(function(){ 
 
    
 
    if(new Date($(this).html()) < new Date()) { 
 
     $(this).css("color", "green"); 
 
    } 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
\t \t <tr> 
 
\t \t \t <td>abc</td> 
 
\t \t \t <td class="dates">2016-10-10</td> 
 
\t \t \t <td>abc</td> 
 
\t \t </tr> 
 
\t \t <tr> 
 
\t \t \t <td>abc</td> 
 
\t \t \t <td class="dates">2016-09-10</td> 
 
\t \t \t <td>abc</td> 
 
\t \t </tr> 
 
\t \t <tr> 
 
\t \t \t <td>abc</td> 
 
\t \t \t <td class="dates">2016-08-10</td> 
 
\t \t \t <td>abc</td> 
 
\t \t </tr> 
 
\t \t <tr> 
 
\t \t \t <td>abc</td> 
 
\t \t \t <td class="dates">2016-11-10</td> 
 
\t \t \t <td>abc</td> 
 
\t \t </tr> 
 
</table>

0

你需要確保你正在處理格式正確的日期,或者使用像Moment.js這樣的庫。如果您正在處理正確的日期,則可以使用new Date()將單元格中的日期與當天的日期進行比較,並有條件地應用某些CSS。

$(document).ready(function(){ 
 
    $('.date').each(function(){ 
 
    if (new Date($(this).text()) > new Date()) { 
 
     $(this).css('background-color', 'red'); 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr><td class="date">Tue Oct 04 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Mon Oct 03 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Fri Oct 07 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Mon Oct 03 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Permanent</td></tr> 
 
    <tr><td class="date">Sat Oct 05 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Sun Oct 07 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Mon Oct 08 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Tue Oct 09 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
    <tr><td class="date">Permanent</td></tr> 
 
    <tr><td class="date">Wed Oct 10 2016 01:24:37 GMT-0600 (MDT)</td></tr> 
 
</table>

相關問題