简体   繁体   English

即使它们不是兄弟姐妹,也可以在某个元素之后查找类

[英]Find class after a certain element even if they are not siblings

I have a list of elements with the same class month .我有一个具有相同类month的元素列表。 Some of them might have the class cal as well, and only one element of the list has the class today .其中一些可能也有类cal ,并且列表中只有一个元素有类today

I'd like to search for the first element with the class cal , but only after the element today is found.我想搜索类cal的第一个元素,但只能在找到today的元素之后。 The tricky part here is that the elements might not be siblings.这里棘手的部分是元素可能不是兄弟元素。 If they were siblings, this would work:如果他们是兄弟姐妹,这将起作用:

 $('#eventCal .today').nextAll('#eventCal .cal:first').text();

So for example, if I have the following list, how could I target the first cal after today ?例如,如果我有以下列表,我如何定位today之后的第一个cal

<div id="eventCal">
    <ul>
        <li class="month"></li>
        <li class="month cal">Not show because it's before TODAY</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month"></li>
        <li class="month today">Today</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month cal">To show as it's the first CAL after the TODAY</li>
        <li class="month cal">Not show because is not the first CAL</li>
        <li class="month"></li>
    </ul>
</div>

To make this work when the li are not all siblings you can get the index of the .today element within all .month elements.为了在li不是所有兄弟姐妹时完成这项工作,您可以在所有.month元素中获取.today元素的索引。 Then you can get all .month following that, and retrieve the first .cal using filter() , like this:然后你就可以得到所有.month紧接着,并检索第一.cal使用filter()就像这样:

 var todayIndex = $('.month.today').index(); var $cal = $(`.month:gt(${todayIndex})`).filter('.cal:first').addClass('foo');
 .foo { color: #C00; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="eventCal"> <ul> <li class="month"></li> <li class="month cal">Not show because it's before TODAY</li> <li class="month"></li> </ul> <ul> <li class="month"></li> <li class="month today">Today</li> <li class="month"></li> </ul> <ul> <li class="month cal">To show as it's the first CAL after the TODAY</li> <li class="month cal">Not show because is not the first CAL</li> <li class="month"></li> </ul> </div>

An alternate solution is, find it by parent plus nextAll like below:另一种解决方案是,通过parentnextAll找到它,如下所示:

 $('.month').each(function() { if ($(this).hasClass('today')) { $(this).parent().nextAll().find('.cal:first').addClass('active'); } });
 .active { background: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="eventCal"> <ul> <li class="month"></li> <li class="month cal">Not show because it's before TODAY</li> <li class="month"></li> </ul> <ul> <li class="month"></li> <li class="month today">Today</li> <li class="month"></li> </ul> <ul> <li class="month cal">To show as it's the first CAL after the TODAY</li> <li class="month cal">Not show because is not the first CAL</li> <li class="month"></li> </ul> </div>

Or you can easily get it like this:或者你可以很容易地得到它:

 $('.today').parent().nextAll().find('.cal:first').addClass('active');
 .active { background: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="eventCal"> <ul> <li class="month"></li> <li class="month cal">Not show because it's before TODAY</li> <li class="month"></li> </ul> <ul> <li class="month"></li> <li class="month today">Today</li> <li class="month"></li> </ul> <ul> <li class="month cal">To show as it's the first CAL after the TODAY</li> <li class="month cal">Not show because is not the first CAL</li> <li class="month"></li> </ul> </div>

    $(document).ready(function(){
      var foundToday = false;

  $('.month').each(function (idx,item) { 
         if( $(this).hasClass('cal') ){
            foundToday = true;
         }

         if(foundToday){
            if( $(this).hasClass('today') ){
                console.log($(this).text())
            }
         }

  });

});

I think the next solution is quite simple.我认为下一个解决方案非常简单。

var first_cal = ''
if($('.today').siblings('.cal').length){
    first_cal = $('.today').siblings('.cal').html()
}else{
    first_cal = $('.today').parent().nextAll('ul').find('.cal').html();
}

$('.result').text(first_cal);

What we do is to check if .today has siblings with cal, if not, we search the first cal starting from the next ul.我们要做的是检查 .today 是否有带有 cal 的兄弟姐妹,如果没有,我们从下一个 ul 开始搜索第一个 cal。

Notice that you can put as many ul without cal as you want after the ul that contains today, it wont affect the result.请注意,您可以在包含今天的 ul 之后放置任意数量的不带 cal 的 ul,它不会影响结果。

Here an example on fiddle : https://jsfiddle.net/Samuel10F/utna63qx/这里有一个关于小提琴的例子https : //jsfiddle.net/Samuel10F/utna63qx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM