在某个元素之后查找类。

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

Find class after a certain element

问题

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

我想要查找具有类名 cal 的第一个元素,但只有在找到元素 today 之后才能查找。

例如,如果我有以下列表:

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

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

So for example, if I have the following list:

&lt;ul id=&quot;eventCal&quot;&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;li class=&quot;month&quot;&gt;&lt;/li&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;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;

答案1

得分: 5

你可以使用以下选择器:

document.querySelector('.today ~ .cal')  $('.today ~ .cal').first();

这将选取第一个.today类之后的第一个.cal类。有关~选择器的更多信息,请参阅What does the “~” (tilde/squiggle/twiddle) CSS selector mean?

$('.today ~ .cal').first().css('color', 'red')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="eventCal">
  <li class="month"></li>
  <li class="month cal">因为在今天之前,所以不显示</li>
  <li class="month"></li>
  <li class="month"></li>
  <li class="month"></li>
  <li class="month today">今天</li>
  <li class="month"></li>
  <li class="month cal">因为是今天之后的第一个CAL,所以显示</li>
  <li class="month cal">因为不是第一个CAL,所以不显示</li>
  <li class="month"></li>
</ul>
英文:

You can use the following selector:

document.querySelector(&#39;.today ~ .cal&#39;) or $(&#39;.today ~ .cal&#39;).first();

This will grab the first .cal class after the first .today class. For more information about the ~ selector, see What does the “~” (tilde/squiggle/twiddle) CSS selector mean?

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

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

$(&#39;.today ~ .cal&#39;).first().css(&#39;color&#39;, &#39;red&#39;)

<!-- 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;ul id=&quot;eventCal&quot;&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;li class=&quot;month&quot;&gt;&lt;/li&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;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;

<!-- end snippet -->

答案2

得分: 0

如果您想要使用jQuery并已经有.today元素,您可以使用nextAll来查找与选择器匹配的所有后续兄弟元素,并将其限制为第一个.cal,使用:first

let today = $('.today')
today.nextAll('.cal:first').css('color', 'red')

如果您想要获取所有后续匹配选择器.cal的兄弟元素,然后可以删除:first

如果您没有today元素,只关心cal元素,您可以使用$('.today ~ .cal').first()

英文:

If you want to do that with jQuery and you already have the .today element, you can use nextAll to find the all following siblings that match the selector and limit it to the first .cal using :first:

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

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

let today = $(&#39;.today&#39;)
today.nextAll(&#39;.cal:first&#39;).css(&#39;color&#39;,&#39;red&#39;)

<!-- 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;ul id=&quot;eventCal&quot;&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;li class=&quot;month&quot;&gt;&lt;/li&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;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;

<!-- end snippet -->

If you want to have all following sibling that match the selector .cal, then you can remove the :first

If you don't have the today element and you are only interested in the the cal element, you can use $(&#39;.today ~ .cal&#39;).first().

答案3

得分: 0

你可以使用 :first 来获取今天的第一个li,然后使用 nextAll:first 来获取今天之后的第一个cal。

$(function(){
  var text = $('#eventCal li.today:first').nextAll('li.cal:first').text();
  console.log(text);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<ul id="eventCal">
  <li class="month"></li>
  <li class="month cal">因为在今天之前所以不显示</li>
  <li class="month"></li>
  <li class="month"></li>
  <li class="month"></li>
  <li class="month today">今天</li>
  <li class="month"></li>
  <li class="month cal">要显示,因为它是今天之后的第一个CAL</li>
  <li class="month cal">不显示,因为不是第一个CAL</li>
  <li class="month"></li>
</ul>
英文:

You can make use of :first to get the first today li and then use nextAll with :first to get first cal after today

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

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

$(function(){
  var text = $(&#39;#eventCal li.today:first&#39;).nextAll(&#39;li.cal:first&#39;).text();
  console.log(text);
});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;ul id=&quot;eventCal&quot;&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;li class=&quot;month&quot;&gt;&lt;/li&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;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;

<!-- end snippet -->

答案4

得分: 0

如果您只想在类名为"today"的元素后面显示第一个"cal",则可以使用以下选择器:

$("#eventCal .month.cal").hide();
$("#eventCal .today").nextAll(".month.cal:first").show();
英文:

If you want to show only the first cal after class today then use the following selector:

 $(&quot;#eventCal .month.cal&quot;).hide();
 $(&quot;#eventCal .today&quot;).nextAll(&quot;.month.cal:first&quot;).show();

答案5

得分: 0

.today ~ :not(.cal) + .cal {
    background: red;
}
英文:

Using only css you can achieve it

By using .today ~ .cal you get all .cal after .today, but this will give you the full list .cal after .today so to exclude all .cal after the first appearance of .cal you can use this :not(.cal) + .cal

so the css selector will be .today ~ :not(.cal) + .cal
<!-- begin snippet: js hide: false console: true babel: false -->

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

.today ~ :not(.cal) + .cal
{
background:red;
}

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

&lt;ul id=&quot;eventCal&quot;&gt;
&lt;li class=&quot;month&quot;&gt;No&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;No&lt;/li&gt;
&lt;li class=&quot;month&quot;&gt;No&lt;/li&gt;
&lt;li class=&quot;month&quot;&gt;No&lt;/li&gt;
&lt;li class=&quot;month today&quot;&gt;Today&lt;/li&gt;
&lt;li class=&quot;month&quot;&gt;No&lt;/li&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;No&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定