遍历DOM从单击的元素获取节点 [英] Traversing up the DOM getting the node from a clicked element

查看:68
本文介绍了遍历DOM从单击的元素获取节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,当我单击页面上的任何元素时,我想做的是以下操作:我想为元素所在的DOM构建一棵树。

OK, what I'm trying to do is the following, when I click on any element on the page and I want to build up a tree of the DOM of where the element lives.

html的示例

<html>
  <head>
  </head>
  <body>
    <ul>
     <li><span>Elem 1</span></li>
     <li><span>Elem 2</span></li>
     <li><span>Elem 3</span></li>
    </ul>
  </body>
</html>

这是JS(来自click事件,并通过clicked元素)

This is the JS (comes from an click event and passes through the clicked element)

function getElementIdentifier(elem, domSelector, firstId) {
    if(elem.tagName === 'HTML') {
        return 'HTML ' + domSelector;
    } else {
        return getElementIdentifier(elem.parentNode, ' > ' +elem.tagName + ' ' + domSelector, firstId);
    }
}

这在一定程度上有效,但涉及到事实上,我已经点击了列表中的第三项,它只会检索以下内容:

which works to an extent but when it comes to the fact that I've clicked on the third item in the list it only retrieves the following:

HTML  > BODY  > UL  > LI  > SPAN

但是我想检索第三个,因为这是我单击的那个。我有一个要演示的codepen。

But I want to retrieve the 3rd as this is the one I clicked. I've got a codepen I did to show.

http://codepen.io/tom-maton/pen/FljpL/

我不想使用jQuery-只是在寻找使用原始js。

I'm not wanting to use jQuery - just looking to use raw js.

推荐答案

这是最快的方法(至少对我来说如此)

function clickHandler(event) {
    var target = event.target,
    breadcrumb = [];

    while (target) {
        breadcrumb.unshift(target.tagName);
        target = target.parentElement;
    }
    console.log(breadcrumb.join(" > "));
}

document.addEventListener('click', clickHandler, false);

在这里拨弄: http://jsfiddle.net/NTEv2/

带有兄弟姐妹的版本:

function getTagName(element) {
    return element.tagName;
}
function clickHandler(event) {
    var target = event.target,
        breadcrumb = [],
        temp;

    while (target) {
        target = target.parentElement;
        if (target) {
            breadcrumb.unshift(([].slice.call(target.children).map(getTagName).join(" + ")));
        }
    }

    // HTML is always there
    breadcrumb.unshift(document.documentElement.tagName);

    console.log(breadcrumb.join(" > "));
}

document.addEventListener('click', clickHandler, false);

在这里拨弄: http://jsfiddle.net/NTEv2/1/

仅具有先前同级的版本(根据要求):

function clickHandler(event) {
    var target = event.target,
        breadcrumb = [],
        part = [],
        prev = target,
        temp;

    while (target) {
        if (prev) {
            part.unshift(prev.tagName);
            prev = prev.previousElementSibling;
        } else {
            target = target.parentElement;
            prev = target;
            breadcrumb.unshift(part.join(" + "));
            part = [];
        }
    }

    console.log(breadcrumb.join(" > "));
}

document.addEventListener('click', clickHandler, false);

在这里拨弄: http://jsfiddle.net/NTEv2/11/

这篇关于遍历DOM从单击的元素获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆