找到在某个元素之后的类,即使它们不是兄弟元素。

huangapple go评论72阅读模式
英文:

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

问题

我有一个具有相同类名 month 的元素列表。其中一些可能还具有类名 cal,并且列表中只有一个元素具有类名 today

我想要查找具有类名 cal 的第一个元素,但只在找到具有类名 today 的元素之后进行。这里的棘手部分是这些元素可能不是兄弟元素。如果它们是兄弟元素,可以这样做:

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

所以例如,如果我有以下列表,如何定位在 today 之后的第一个 cal

<div id="eventCal">
    <ul>
        <li class="month"></li>
        <li class="month cal">因为它在今天之前,所以不显示</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month"></li>
        <li class="month today">今天</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month cal">因为它不是第一个CAL,所以不显示</li>
        <li class="month cal">要显示,因为它是今天之后的第一个CAL</li>
        <li class="month"></li>
    </ul>
</div>

你可以使用以下代码来实现:

$('#eventCal .today').parent().nextAll().find('.cal:first').text();
英文:

I have a list of elements with the same class month. Some of them might have the class cal as well, and only one element of the list has the class today.

I'd like to search for the first element with the class cal, but only after the element today is found. The tricky part here is that the elements might not be siblings. If they were siblings, this would work:

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

So for example, if I have the following list, how could I target the first cal after today?

&lt;div id=&quot;eventCal&quot;&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because it&#39;s before TODAY&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month today&quot;&gt;Today&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month cal&quot;&gt;To show as it&#39;s the first CAL after the TODAY&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because is not the first CAL&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

答案1

得分: 2

为了使这个代码在li不都是兄弟元素时也能正常工作,你可以获取.today元素在所有.month元素中的索引。然后,你可以获取所有在这之后的.month元素,并使用filter()检索第一个.cal元素,就像这样:

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">不显示,因为在今天之前</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month"></li>
        <li class="month today">今天</li>
        <li class="month"></li>
    </ul>
    <ul>
        <li class="month cal">显示,因为是今天之后的第一个CAL</li>
        <li class="month cal">不显示,因为不是第一个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. Then you can get all .month following that, and retrieve the first .cal using filter(), like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var todayIndex = $(&#39;.month.today&#39;).index();
var $cal = $(`.month:gt(${todayIndex})`).filter(&#39;.cal:first&#39;).addClass(&#39;foo&#39;);

<!-- language: lang-css -->

.foo { color: #C00; }

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;eventCal&quot;&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because it&#39;s before TODAY&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month today&quot;&gt;Today&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month cal&quot;&gt;To show as it&#39;s the first CAL after the TODAY&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because is not the first CAL&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的部分:

第一个替代方案是,通过以下方式通过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">因为它在今天之前,所以不显示</li>
    <li class="month"></li>
  </ul>
  <ul>
    <li class="month"></li>
    <li class="month today">今天</li>
    <li class="month"></li>
  </ul>
  <ul>
    <li class="month cal">要显示,因为它是今天之后的第一个CAL</li>
    <li class="month cal">因为不是第一个CAL,所以不显示</li>
    <li class="month"></li>
  </ul>
</div>

或者你可以这样轻松地获取它:

$('.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">因为它在今天之前,所以不显示</li>
    <li class="month"></li>
  </ul>
  <ul>
    <li class="month"></li>
    <li class="month today">今天</li>
    <li class="month"></li>
  </ul>
  <ul>
    <li class="month cal">要显示,因为它是今天之后的第一个CAL</li>
    <li class="month cal">因为不是第一个CAL,所以不显示</li>
    <li class="month"></li>
  </ul>
</div>
英文:

An alternate solution is, find it by parent plus nextAll like below:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.month&#39;).each(function() {
  if ($(this).hasClass(&#39;today&#39;)) {
    $(this).parent().nextAll().find(&#39;.cal:first&#39;).addClass(&#39;active&#39;);
  }
});

<!-- language: lang-css -->

.active {
  background: red;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;eventCal&quot;&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because it&#39;s before TODAY&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month today&quot;&gt;Today&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month cal&quot;&gt;To show as it&#39;s the first CAL after the TODAY&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because is not the first CAL&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

Or you can easily get it like this:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

$(&#39;.today&#39;).parent().nextAll().find(&#39;.cal:first&#39;).addClass(&#39;active&#39;);

<!-- language: lang-css -->

.active {
  background: red;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;eventCal&quot;&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because it&#39;s before TODAY&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
        &lt;li class=&quot;month today&quot;&gt;Today&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul&gt;
        &lt;li class=&quot;month cal&quot;&gt;To show as it&#39;s the first CAL after the TODAY&lt;/li&gt;
        &lt;li class=&quot;month cal&quot;&gt;Not show because is not the first CAL&lt;/li&gt;
        &lt;li class=&quot;month&quot;&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

$(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());
        }
     }
  });
});
英文:
    $(document).ready(function(){
      var foundToday = false;

  $(&#39;.month&#39;).each(function (idx,item) { 
   		 if( $(this).hasClass(&#39;cal&#39;) ){
         	foundToday = true;
         }
         
         if(foundToday){
          	if( $(this).hasClass(&#39;today&#39;) ){
         		console.log($(this).text())
         	}
         }
        
  });
  
});

答案4

得分: 0

我认为下一个解决方案非常简单。

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);

我们的做法是检查.today是否有具有.cal类的兄弟元素,如果没有,我们从下一个ul开始搜索第一个具有.cal类的元素。

请注意,您可以在包含.todayul之后放置任意多个不包含.calul,这不会影响结果。

这里有一个在fiddle上的示例: https://jsfiddle.net/Samuel10F/utna63qx/

英文:

I think the next solution is quite simple.

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

$(&#39;.result&#39;).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.

Notice that you can put as many ul without cal as you want after the ul that contains today, it wont affect the result.

Here an example on fiddle: https://jsfiddle.net/Samuel10F/utna63qx/

huangapple
  • 本文由 发表于 2020年1月6日 19:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定