jquery - How do i select every last child of a specific class in a list -
i have div contains list of other div messages, want select every last child of div has same class.
here code:
<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> all want select every last child of class "q".
like this:
<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 please no matter if can done through css, jquery or php snippet
this worked. these 2 selectors.
$("p.q + p:not(p.q)").prev("p"); $("p.q:last"); or single one:
$('p.q + :not(p.q)').prev().add('p.q:last'); could complicated, worth try:
$(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>
Comments
Post a Comment