仅使用 Javascript,我如何遍历每个 span id = "expiredDate"?我认为使用 for each 循环会很容易,但它似乎不存在于 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>