CSS selector for the last occurrence of the class on the page

Is there a CSS selector for the last occurrence of the class on the page?

Say I have this HTML

<dd>
    <span>
        <a class="required" id="forename">foo</a>
    </span>
</dd>
<dd>
    <span>
        <a class="required" id="surname">bar</a>
    </span>
</dd>

Is there a CSS selector that returns a tag awith a surname identifier. Something like .required:lastmaybe?

Will Prototype be used if that matters?

+3
source share
3 answers

Using prototype:

var sel = $(document.body).select('a.required').toArray()
var last = sel[sel.length-1]

(rotated: var last = sel.last())

Perhaps there will be an easier way.

+2
source

CSS3 has a last-child selector, but not many browsers support it.

If you can use jquery you can do:

$("a.required:last")
+2
source

:last-of-type ( .required ), spotty. (Edit: no it not not, last-of-type .) Javascript, , last HTML.

@snaken: :last-child ( CSS2) - : , . .

<div>
  <a class="required">...</a>
  <a class="required">...</a>
  <a>...</a><!-- this is :last-child! -->
</div>

required:last-child , .required .

0

Source: https://habr.com/ru/post/1745172/


All Articles