感谢大家考虑我的新手问题。 我正在使用 jQuery 迭代表并捕获数组中的所有硬编码日期。 我将这些日期与 Date.today(); 进行比较使用 .isAfter() 函数。 如果硬编码的日期是过去的,它应该得到一个 .css("display", "none");
<table>
<html>
<table>
<tr class="gig">
<td class="gigDate">Mar 27 2013</td>
<td>the Saloon</td>
<td>Atlanta, Ga</td>
<td>$20 (2 passes)</td>
<td>button to purchase tix<td>
</tr>
<tr class="gig"><td class="gigDate"> ... date2 ..</td></tr>
<tr class="gig"><td class="gigDate"> ... date3 ..</td></tr>
<tr class="gig"><td class="gigDate"> ... date4 ect ..</td></tr>
</table>
<script type="text/javascript">
$("document").ready( function() {
var stringDate = []; //create array to hold elements
var now = Date.today(); // create current date
$('.gigDate').each(function (i, e) { //adds hardcoded dates to array
stringDate.push($(e).text());
});
for(var y = 0; y < stringDate.length; y++){
var singleDate = Date.parse(stringDate[y]); //parse to milliseconds
var compare = now.isAfter(singleDate); //compare to today's date
if(compare){
$('.gig').css("display", "none"); //hide <tr class="gig">
//if in the past
}
}
});
</script>
循环逻辑似乎运行正常,但添加样式 display:none 的 if(语句) 很糟糕。 任何帮助将不胜感激。
最佳答案
当您尝试隐藏一行时,您只需隐藏所有行 - $('.gig') 选择所有行,而不仅仅是您认为应该选择的行。
将逻辑移至测试日期的循环内。大致是这样的(我尽可能少地改变你的代码,可能仍然不起作用,没有检查):
var now = Date.today(); // create current date
$('.gigDate').each(function (i, e) { //adds hardcoded dates to array
var current = ($(e).text());
var singleDate = Date.parse(current); //parse to milliseconds
var compare = now.isAfter(singleDate); //compare to today's date
if(compare){
// we want to hide the parent of td, not the td:
$(e).parent().css("display", "none"); //hide <tr class="gig">
//if in the past
}
});
关于jquery:遍历表将<td>硬编码日期</td>与Date.today()进行比较,然后删除日期<today,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15648810/