简体   繁体   English

查找 DOM 中元素之间的距离

[英]Find distance between elements in the DOM

There is a root element in the DOM tree and there is another element inside this root element nested somewhere.在 DOM 树中有一个根元素,并且在这个根元素中嵌套了另一个元素。 How do I calculate how nested is this another element inside the root element?我如何计算根元素内的另一个元素是如何嵌套的?

What I would like to know is essentially how many times I have to get the parent element of the nested element until I get to the root element.我想知道的是,在到达根元素之前,我必须获得嵌套元素的父元素多少次。 So I can solve this problem by iterating on the parents until I get to the root element, like in this fiddle .所以我可以通过迭代父元素来解决这个问题,直到我到达根元素,就像在这个fiddle 中一样

 const first = document.getElementById('search-target-1'); let parent = first.parentElement; let level = 0; do { parent = parent.parentElement; level++; } while (!parent.classList.contains('root')) console.log(`The first element is ${level} levels deep inside the root div.`); const second = document.getElementById('search-target-2'); parent = second.parentElement; level = 0; do { parent = parent.parentElement; level++; } while (!parent.classList.contains('root')); console.log(`The second element is ${level} level deep inside the root div.`);
 <div class="root"> <div class="first-level"> <div class="second-level" id="search-target-1"> <!-- How deep is this element? --> </div> </div> <div class="first-level"></div> <div class="first-level"> <div class="second-level"> <div class="third-level" id="search-target-2"> <!-- And this one? --> </div> </div> </div> </div>

Is there a better way of achieving this?有没有更好的方法来实现这一目标? I am looking for a javascript api to get the same result.我正在寻找一个 javascript api 来获得相同的结果。

The element.matchesSelector does not solve my problem, as I know the target element is inside the root element, but I don't know how deep it is.该element.matchesSelector解决我的问题,因为我知道这个目标元素根元素里面,但我不知道它是多么深。

You could use jQuery's .parentsUntil() function to accomplish this:您可以使用 jQuery 的.parentsUntil()函数来完成此操作:

var searchDistance_1 = $("#search-target-1").parentsUntil(".root").length; // 1
var searchDistance_2 = $("#search-target-2").parentsUntil(".root").length; // 2

That gives you the number of parents in between the child and root you are looking for.这为您提供了您正在寻找的孩子和根之间的父母数量。 If you're looking for the number of jumps up the hierarchy needed to get to the parent, you can just add 1.如果您正在寻找到达父级所需的层次结构的跳转次数,则只需添加 1。

If you need to do this in vanilla JS, you could look through the source code for this function on GitHub.如果您需要在 vanilla JS 中执行此操作,您可以在 GitHub 上查看此功能的源代码

Your code works, but as Niet the Dark Absol said you need to take care of cases where the descendent isn't a descendent, eg您的代码有效,但正如 Niet the Dark Absol 所说,您需要处理后代不是后代的情况,例如

 function getLevel(parent, child) { let level = 0; while (child && parent != child) { level++; child = child.parentNode; } return child? level : null; } var parent = document.getElementById('parent'); var child = document.getElementById('child'); var notChild = document.getElementById('notChild'); console.log(getLevel(parent, child)); // 3 console.log(getLevel(parent, notChild)); // null
 <div id="parent"> <div> <div> <div id="child"></div> </div> </div> </div> <div id="notChild"></div>

Without the guarding condition to stop when the loop runs out of parnetNodes, it will throw an error if child isn't a descendant of parent .如果没有在循环用完 parnetNodes 时停止的保护条件,如果child不是parent的后代,它将抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM