Can anyone explain to me what the heck is going on with the jQuery .is() function?
I have a situation where I’m checking to see if an element is the first of a group of elements that I have selected.
<div>
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
<li>last</li>
</ul>
</div>
$('div > ul > li').first().is(':first') or
$('div > ul').find('li:first').is('li:first')
returns false…. what is going on here?
$('div > ul > li').last().is(':last') or
$('div > ul').find('li:last').is('li:last')
return true
The selector
:firstjust selects the first element from all elements on the page. That one is<html>, try running$(":first").:lastworks in your case because the last<li>also happens to be the last element on the whole page.You might want
:first-of-typeor:first-childinstead.