-3
a = $('div:first');
b = $('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)?

new_perl
  • 7,345
  • 11
  • 42
  • 72
  • 4
    Your question is really unclear. What is "last element of `b`"? And which sibling are you referring to? – BoltClock Sep 27 '11 at 06:05
  • Could you perhaps edit in some example HTML? – Hubro Sep 27 '11 at 06:08
  • Assuming "element of" means "child of" (your question is still confusing), `div:first` can **never** be a child of another `div` since `div:first` would select the very first, outermost `div` in the document. – BoltClock Sep 27 '11 at 06:17
  • @BoltClock,that's just an example ,the problem is how I can write a `is_a_last_of_b(a, b)`. – new_perl Sep 27 '11 at 06:18
  • Tried to fix your title, edit if this is not correct. Anything is better than "Question about jQuery" though, can't get much more vague than that. I don't get `is_a_last`, how many **last** elements do you expect? – Wesley Murch Sep 27 '11 at 06:21
  • @bitsMix,it has nothing to do with html – new_perl Sep 27 '11 at 06:22
  • @Wesley Murch ,I know how to select the last ,just put `:last` in the selector. – new_perl Sep 27 '11 at 06:24

3 Answers3

1

Can try this

var elements = $("div");
var lastElement = elements.filter(":last");
var firstElement = elements.filter(":first");

For sibling check this out jquery siblings

Wazy
  • 8,822
  • 10
  • 53
  • 98
0

Maybe you need last-selector and siblings?

Ya Zhuang
  • 4,652
  • 31
  • 40
0

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();
Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721