Reputation: 436
I use jQuery is(":first") to judge whether an element is the first in its siblings, but it always return false. Is there any wrong? or is it a bug of jQuery? thx.
$(".page:first").is(":first"); //always return false.
Upvotes: 1
Views: 111
Reputation: 173642
To test whether the first found .page
element is the first sibling you can use .index()
:
if ($('.page:first').index() == 0) {
// it's the first child
}
Or simply:
if ($('.page').index() == 0) {
// it's the first child
}
The .is()
function works by matching the current result set against the given selector, which is the first element of the document, i.e. <html>
. Only $('html').is(':first')
will be true
.
Upvotes: 2
Reputation: 2459
You want to check if the first element of .page
is the first element. According to your code, we cannot know whose element's first child.
If you check output of $(":first")
, it gives the top element of page which is <html>
tag.
Therefore, your check will always return false. Specify more about what you want to do or this logic will always fail.
Upvotes: 1
Reputation: 3142
Jquery has a method called first()
. So try to use $(".page").first()
Upvotes: 0
Reputation: 85575
You should use like this:
$(".page").is(":first") //always return false.
Or,
$(".page:first") //always return false.
Or,
$(".page").first() //always return false.
Upvotes: 1