2014-02-20 77 views
0

我试图运行一个if语句使用jQuery。我相信我遵循了正确的结构,但代码没有运行。我的jQuery如果其他语句不工作

我相信它与语句的性质有关,在语句中,无论我写的if else语句如何都会覆盖另一个语句。

最后其他的if语句有效,但中间的语句不起作用?

这里是http://jsfiddle.net/gdcx7/

见下面的代码小提琴

<body> 
<div class="thumb-holder"> 
    <div class="post-title" data-date="Tuesday,14 December 2013" data-time="18:00"> 
     <img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" /> 
    </div> 
</div> 
<div class="thumb-holder"> 
    <div class="post-title" data-date="Tuesday,14 December 2012" data-time="18:00"> 
     <img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" /> 
    </div> 
</div> 

<div calss="thumb-holder"> 
    <div class="post-title" data-date="Tuesday,14 December 2012" data-time="18:00"> 
     <img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" /> 
    </div> 
</div> 

<div class="thumb-holder"> 
    <div class="post-title" data-date="Thursday,27 February 2014" data-time="18:00"> 
     <img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" /> 
    </div> 
</div> 


<div class="thumb-holder"> 
    <div class="post-title" data-date="Thursday,25 December 2014" data-time="18:00"> 
     <img class="featured-thumb" src="http://web-vassets.ea.com/Assets/Richmedia/Image/FeaturedGame/crysis3_298x168.jpg?cb=1334583481" alt="tag test" style="width:250px;height:156px" /> 
    </div> 
</div> 

$('.post-title').each(function() { 
    //1 day old 
    if (new Date($(this).data('date')).getTime() < new Date().getTime()) { 
     $(this).css({ 
     opacity: 0.5, 
     border: '2px solid blue' 
     }); 
    } 

    //older than 7 days 
    else if (new Date($(this).data('date')).getTime() < new Date().getTime() - (24 * 7) * 60 * 60 * 1000) { 
    $(this).css({ 
     opacity: 0.5, 
     border: '2px solid red' 
    }); 
    } 

    //7 days to go 
    else if (new Date($(this).data('date')).getTime() < new Date().getTime() + (24 * 7) * 60 * 60 * 1000) { 
    $(this).css({ 
     opacity: 0.5, 
     border: '2px solid green' 
    }); 
    } 
}); 

回答

0

这是因为你做的至少约束第一:

如果是在过去,然后 如果少于7天然后 如果少于7天

冷杉toff,注意最后两个条件是相同的。据我了解,最后一个是未来?如果是的话,改变你的测试标志,把它转化为>

这一切说,你应该扭转ifses并结了:

如果将来再 // 否则,如果在过去的过去7天然后 // 然后 // 7天以上) 结束

其他注意事项: - 请,存储在一个局部变量来使代码lisible和功率不挨饿的日期; - 现在需要时间第一(为同样的原因)加,ECH一次调用的getTime,由当时的结果变化及时调用跨度(它的毫秒,但是......)

+0

你好,不要紧紧跟着你。 _“如果是在过去,然后如果少于7天,然后如果少于7天” _ 的代码是实现1日龄那么7天的并且将来日期 – Kulerbox

0

使用以下条件的,如果保留制度

if (new Date($(this).data('date')).getTime() < new Date().getTime() && new Date($(this).data('date')).getTime() > new Date().getTime() - (24 * 1) * 60 * 60 * 1000) 
    { 
     ----- 
    } 

休息很好。这将检查测试时间是否属于过去的24小时,即1天。否则,你的if条件是检查时间戳是否小于现在意味着现在的任何时间。

也在上如果条件可以是─

else if (new Date($(this).data('date')).getTime() < new Date().getTime() + (24 * 7) * 60 * 60 * 1000 && new Date($(this).data('date')).getTime() > new Date().getTime()) 
    { 
     ------ 
    } 

即如果测试时间少于未来7天&比目前的时间大。

+0

谢谢,我也尝试这些,但我得到同样的确切效果 - 一个陈述覆盖了其他陈述 - 它是否与通过时间戳有关? – Kulerbox