gpt4 book ai didi

jquery - 如何选择列表中特定类(class)的每个最后一个 child

转载 作者:行者123 更新时间:2023-12-01 07:47:23 25 4
gpt4 key购买 nike

我有一个 div,其中包含其他 div 作为消息的列表,我想选择具有相同类的 div 的每个最后一个子级。

这是我的代码:

<p>The first paragraph.</p>
<p class="q">The second paragraph.</p>
<p>The third paragraph.</p>
<p class="q">The fourth paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p>The seventh paragraph.</p>
<p>The seventh paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p>

我想要的只是选择“q”类的每个最后一个子级

像这样:

<p>The first paragraph.</p>
<p class="q">The second paragraph.</p> - Selected
<p>The third paragraph.</p>
<p class="q">The fourth paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p> - Selected
<p>The seventh paragraph.</p>
<p>The seventh paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p> - Selected

无论是否可以通过 css、jQuery 或 PHP 片段完成,请帮忙

最佳答案

这有效。这是两个选择器。

$("p.q + p:not(p.q)").prev("p");
$("p.q:last");

或者作为一个单独的:

$('p.q + :not(p.q)').prev().add('p.q:last');

可能很复杂,但值得一试:

$(function () {
$('p.q + :not(p.q)').prev().add('p.q:last').addClass("selected");
});
.selected {background: #ccf;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>The first paragraph.</p>
<p class="q">The second paragraph.</p>
<p>The third paragraph.</p>
<p class="q">The fourth paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p>The seventh paragraph.</p>
<p>The seventh paragraph.</p>
<p class="q">The fifth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p>
<p class="q">The sixth paragraph.</p>

关于jquery - 如何选择列表中特定类(class)的每个最后一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34623793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com