2011-05-02 70 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();   
    }); 
}); 
相关问题