0

I have both jQuery objects to the ancestor parent and the child element.

$ancestor = ...;
$child = ...;

How do I get the distance between the two? I.e. how many elements are in this ancestry chain?

4

2 に答える 2

6

それらの要素を含む他の2つの要素の間に要素を取得するには:

var $els = $child.parentsUntil($ancestor).andSelf()

または、間にある要素の数を取得するには、次のようにします。

var elsInBetween = $child.parentsUntil($ancestor).length - 1
于 2012-07-08T21:19:28.147 に答える
0

Maybe this will be of assistance -

var counter = 0;
var target = '';
while($child.parent().length){
  target = $child.parent();
  counter++;
}
console.log(target);

I am simply iterating over the parent() object each time round replacing each time with its parent. You'll get ALL the ancestors here... And I mean all of them.

于 2012-07-08T21:17:19.900 に答える