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?
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?
それらの要素を含む他の2つの要素の間に要素を取得するには:
var $els = $child.parentsUntil($ancestor).andSelf()
または、間にある要素の数を取得するには、次のようにします。
var elsInBetween = $child.parentsUntil($ancestor).length - 1
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.