2

我需要用每个名为 event_month 的 div 内的事件类来计算 div 的数量。

我现在有这个:

$('#events #event_listings .event_month').each(function(i, obj) {
    var Count = $(this +'> .event').size();
    console.log(Count); 
});

这是我试图将 Javascript 应用到的 HTML:

<div class="header"><span>October 2012</span></div>
  <div class="event">
    <h3>Telematics Munich 2012 (29th to 30th)</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer commodo ante iaculis purus placerat sed consectetur odio venenatis. Ut erat nisi, laoreet at tempor vel, sollicitudin sed dui.</p>
  </div>
</div>
<div id="september2012" class="event_month">
  <div class="header"><span>September 2012</span></div>
</div>
<div id="august2012" class="event_month">
  <div class="header"><span>August 2012</span></div>
</div> 
<div id="july2012" class="event_month">
  <div class="header"><span>July 2012</span></div>
</div> 
<div id="june2012" class="event_month">
  <div class="header"><span>June 2012</span></div>
  <div class="event">
    <h3>Telematics Detroit 2012 (6th to 7th)</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer commodo ante iaculis purus placerat sed consectetur odio venenatis. Ut erat nisi, laoreet at tempor vel, sollicitudin sed dui.</p>
  </div>
</div>
<div id="may2012" class="event_month">
  <div class="header"><span>May 2012</span></div>
  <div class="event">
    <h3>Insurance Telematics Europe 2012 (9th to 10th)</h3>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer commodo ante iaculis purus placerat sed consectetur odio venenatis. Ut erat nisi, laoreet at tempor vel, sollicitudin sed dui.</p>
  </div>
</div>

不用说,它不起作用。有任何想法吗?

4

3 回答 3

5

尝试以下操作:

$('#event_listings .event_month').each(function(i, obj) {
    var count = $(this).find('.event').length;
    console.log(count); 
});
于 2012-07-21T01:57:12.357 回答
1

this是一个对象,所以你不能用字符串连接它。将其包装在 jQuery 中,然后过滤其子项。

var Count = $(this).children('.event').length;
于 2012-07-21T02:01:53.553 回答
1

尝试这个

$(function(){
    $('div.event_month').each(function(){
        alert($(this).children('.event').length);
    });​
  }
);​

这是一个功能示例:http: //jsfiddle.net/Ug7SF/

于 2012-07-21T02:05:22.473 回答