how do I write a `is_a_last_of_b(a, b)` to check whether `a` is the last child of `b`
a = $('div:first');
b开发者_如何学JAVA = $('div');
b
may contain multiple DOM elements.
But a
only contains 1 element.
How do I know if a
is the last element of b
?
I tried a == $('div:last')
,but it's wrong after my test.
And if a
is an element of b
, how do I get the sibling of a
within b
?
Clarify
the problem is how I can write a is_a_last_of_b(a, b)
to check whether a
is the last child of b
.
And if a
is a direct child of b
,how to write next_sibling(a)
to return the next sibling of a
(only one)?
Can try this
var elements = $("div");
var lastElement = elements.filter(":last");
var firstElement = elements.filter(":first");
For sibling check this out jquery siblings
Maybe you need last-selector and siblings?
Ok, to compare jquery objects, you can look at some other questions on SO like this one: jQuery object equality
Essentially, in your case where you only want to check for one item you could do this:
var a = $("div:first"),
b = $("div");
if (b[b.length - 1] === a[0]) {}
// or
if (b.last()[0] = a[0]) {}
Then, to get the next sibling of an element:
b = b.next();
精彩评论