2014-10-02 83 views
1

我一直在试图解决这个问题两天。也许有人对我有暗示?jquery /比较日期列表

我有这对夫妇的每个div.news-list-item是2日期 - startdateenddate。我想比较两个日期来检查它们是否相等。如果他们相同,则添加类show,如果不做别的。

问题是startDate始终为空或未定义。

<div class="news-list-container row"> 
    <div class="news-list-item nth-item-1"> 
     <span class="event-from">17.10.2014</span> 
     <span class="event-to">19.10.2014</span> 
    </div> 

    <div class="news-list-item nth-item-2"> 
     <span class="event-from">07.12.2014</span> 
     <span class="event-to">07.12.2014</span> 
    </div> 

    <div class="news-list-item nth-item-3"> 
     <span class="event-from">08.12.2014</span> 
     <span class="event-to">08.12.2014</span> 
    </div> 
</div> 



$('.news-list-container').each(function() { 
    var $children = $(this).children(), 
     count = $children.size(), 
     $item; 
     $children.each(function(i) { 
      $item = $(this).addClass('nth-item-' + (i + 1)) 

}); 

$(".news-list-item").each(function() { 
    var startDate = $(".event-from").val(); 
    var endDate = $(".event-to").val(); 

     if(startDate == endDate) { 
       $(this).addClass("show");  
      } else { 

      } 
    }); 
    console.log("story " + startDate + " story"); 
    }); 
}); 

回答

0
  • 使用find相对尝试当前$(this)
  • 使用text检索的文本内容

这里工作生活样本:

$('.news-list-container').each(function() { 
 
    var $children = $(this).children(), count = $children.size(), $item; 
 
    $children.each(function(i) { 
 
     $item = $(this).addClass('nth-item-' + (i + 1)) 
 
    }); 
 

 
    $(this).find(".news-list-item").each(function() { 
 
     var startDate = $(this).find(".event-from").text(); 
 
     var endDate = $(this).find(".event-to").text(); 
 

 
     if(startDate == endDate) { 
 
      $(this).show();  
 
     } else { 
 
      $(this).hide(); 
 
     } 
 
     console.log("story " + startDate + " story"); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="news-list-container row"> 
 
    <div class="news-list-item nth-item-1"> 
 
     <span class="event-from">17.10.2014</span> 
 
     <span class="event-to">19.10.2014</span> 
 
    </div> 
 

 
    <div class="news-list-item nth-item-2"> 
 
     <span class="event-from">07.12.2014</span> 
 
     <span class="event-to">07.12.2014</span> 
 
    </div> 
 

 
    <div class="news-list-item nth-item-3"> 
 
     <span class="event-from">08.12.2014</span> 
 
     <span class="event-to">08.12.2014</span> 
 
    </div> 
 
</div>

+1

这是一个完美的例子,如何做到这一点。我做了两个小修改,但不值得说,因为我没有在我的问题中描述它。非常感谢尼古拉斯。 – bluebyte 2014-10-02 08:47:47

1

您需要thiscontext添加到$(“事件 - 来自‘)和$(’事件到”),以获得孩子的div。

此外跨度没有VAL(),但文本()

var startDate = $(".event-from",this).text(); 
var endDate = $(".event-to",this).text(); 

这是一样的

$(this).find("span.event-from")... 

这里更多:How to get the children of the $(this) selector?

我的例子

$(function() { 
 
    $(".news-list-container > .news-list-item").each(function() { 
 
    var startDate = $(".event-from",this).text(); 
 
    var endDate = $(".event-to",this).text(); 
 
    $(this).toggle(startDate != endDate); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="news-list-container row"> 
 
    <div class="news-list-item nth-item-1"> 
 
     <span class="event-from">17.10.2014</span> 
 
     <span class="event-to">19.10.2014</span> 
 
    </div> 
 

 
    <div class="news-list-item nth-item-2"> 
 
     <span class="event-from">07.12.2014</span> 
 
     <span class="event-to">07.12.2014</span> 
 
    </div> 
 

 
    <div class="news-list-item nth-item-3"> 
 
     <span class="event-from">08.12.2014</span> 
 
     <span class="event-to">08.12.2014</span> 
 
    </div> 
 
</div>

0

您可以在此

$(function(){ 

    $('.news-list-item').each(function() { 

     if($('.event-from', this).text() == $('.event-to', this).text()) { 

      $(this).addClass('show); 

     } else { 

      console.log('Not Equal'); 
     } 

    }); 

});