2011-05-02 98 views
3

我有一個日曆,下面有一個事件列表,它顯示在我的頁面上。當用戶點擊下一個或上一個按鈕時,我想用僅包含當前顯示的月份和年份的日曆中的行內容填充div。這裏是行的結構(用Drupal 7 Views創建)。jQuery:查找包含特定文本的元素

<div id="all-events"> 
    <div class="views-rows"> 
    <div class="field-date"> 
     <div class="field-content"> 
     <span>June 2011</span> 
     </div> 
    </div> 
    </div> 
    <div class="views-rows"> 
    <div class="field-event-title"> 
     <div class="field-content"> 
     <span>Some Event</span> 
     </div> 
    </div> 
    </div> 
</div> 

我走到這一步與jQuery,但我發現了一個巨大的錯誤列表:

$('#calendar span.fc-button-next').live('click', function(){ 
     var month = $('#calendar .fc-header-title h2').html(); 
     $('.event-list').fadeOut(350, function(){ 
     $(this).html(''); 
     $('#all-events').each('.views-rows',function(){ 
      // Code goes here... 
     }); 
     }); 
    }); 

我還要提到的是我使用FullCalendar創建日曆,所以它的創建與這個插件。現在我可以獲取用戶正在查看的當前月份,但是我想查看列表並查找包含這個月的所有行並將它們顯示在.event-list div中。

回答

1

這似乎是錯誤的:

$('#all-events').each('.views-rows',function(){...}); 

如何:

$('.views-rows', '#all-events').each(function(){...}); 
2

我不能完全理解你想要什麼,而是要通過篩選的文本元素使用filter

<div>text 1</div> 
<div> not this text </div> 

JS

$("div").filter(function(){ 
    return $(this).text().indexOf("text 1") >= 0; // if contains desired text 
}).html("this one matches 'text 1'"); 

See this example on jsFiddle

+0

當有多個div包含'text 1'時,它也會替換div,如下所示:http://jsfiddle.net/Rd8jK/1/ – 2013-06-09 01:31:25

0

由於版本1.1.4,jQuery有一個contains selector以選擇包含特定的文本元素。

$('#calendar span.fc-button-next').live('click', function(){ 
    var month = $('#calendar .fc-header-title h2').html(); 
    $('.event-list').fadeOut(350, function(){ 
    var event_list = $(this).html(''); 
    $('#all-events .views-rows:contains(' + month + ')').each(function(){ 
     event_list.append($(this).html); 
    }); 
    event_list.fadeIn();   
    }); 
});