僅使用Javascript,如何遍歷每個跨度id =「expiredDate」?我認爲這對於每個循環來說都會很容易,但它似乎並不存在於JavaScript中。我的下面的代碼只適用於第一個元素。使用javascript遍歷跨度
<script type="text/javascript">
window.onload = function() {
var definedDate = window.document.getElementById('expiredDate').innerHTML.split("-");
var expiredDate = new Date(definedDate[0], definedDate[1] - 1, definedDate[2]);
var graceDate = new Date(definedDate[0], definedDate[1] - 1, definedDate[2] - 30);
var currentDate = new Date();
if(currentDate > expiredDate) {document.getElementById('expiredDate').style.color = '#cc6666';}
else if(currentDate < expiredDate && currentDate > graceDate) {document.getElementById('expiredDate').style.color = '#f0c674';}
else {document.getElementById('expiredDate').style.color = '#b5bd68';}
}
</script>
<span id="expiredDate">2013-01-01</span>
<span id="expiredDate">2014-01-01</span>
<span id="expiredDate">2015-01-01</span>
如果您需要有效的html,應該只有一個''span''''expiredDate''。 – Kyle
@凱爾 - 足夠公平,這可以用類來完成嗎? – bswinnerton
*應該*與類 – Jason