将类添加到对if语句为true但与其他元素具有相同类的特定项

时间:2015-03-02 23:28:54

标签: jquery if-statement addclass

我正在为我的学校设立奖学金网站,如果奖学金截止日期已过,我想在奖学金中增加一个课程。每个奖学金都有.month。

HTML

<li class="month 11 start" data-due-date="11/11/2014 1:00">
    <time class="cbp_tmtime"><span>Nov. 11, 2014</span></time>
    <div class="cbp_tmicon"></div>
    <div class="cbp_tmlabel">
        <h2>November</h2>
        <p>Text</p>
        <br>
        <a href="#">Website</a> | <a href="#">PDF</a>
    </div>
</li>

<li class="month 12 start" data-due-date="12/11/2014 1:00">
    <time class="cbp_tmtime"><span>Dec. 11, 2014</span></time>
    <div class="cbp_tmicon"></div>
    <div class="cbp_tmlabel">
        <h2>December</h2>
        <p>Text</p>
        <br>
        <a href="#">Website</a> | <a href="#">PDF</a>
    </div>
</li>

Jquery的

function getData(){
     var scholarship = $('.month');
     var data = $(scholarship).data("due-date");
     var dataDate = new Date(data);
     var today = new Date();


     if(dataDate < today){

     }

} 

我已经找了几天方法来做到这一点,但我无法弄明白。这是我得到的。请帮助:/这一直让我疯狂。谢谢!

1 个答案:

答案 0 :(得分:1)

您需要对每个.month元素进行测试

function getData(){
     var scholarships = $('.month'),
         today = new Date();

     scholarships.each(function(){
         var scholarship = $(this),
             data = scholarship.data("due-date"),
             dataDate = new Date(data);

         if(dataDate < today){
             scholarship.addClass('expired');
         }
     });
}