感谢所有人考虑我的新问题。我使用jQuery遍历一个表,并捕获数组中的所有硬编码日期。我使用.isAfter()函数将这些日期与Date.today()进行比较。如果硬编码的日期是过去的,应该会得到一个.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(语句)是错误的。任何帮助都将不胜感激。
发布于 2013-03-27 06:54:08
当你试图隐藏一行时,你只需隐藏所有行-- $('.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
}
});发布于 2013-03-27 06:48:07
我不知道这个isAfter函数,但您可以直接比较日期。我想那会更容易些。
发布于 2013-03-27 06:50:39
你在客户端过滤有什么原因吗?看起来效率很低。你的html是硬编码的吗?还是你有服务器端的脚本?除非别无选择,否则你真的应该在渲染前过滤你的结果。
https://stackoverflow.com/questions/15648810
复制相似问题