返回元素的完整路径?

编程入门 行业动态 更新时间:2024-10-24 06:33:53
本文介绍了返回元素的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种方法来查找点击时元素的完整路径。

I am looking for a way to find the full path to an element on click.

例如,假设我有这个HTML代码:

For example, lets say I have this HTML code:

<div> <ul> <li>item 1</li> <li>item 2</li> </ul> </div> <div> <ul> <li>item 1</li> <li>item 2</li> </ul> </div>

我希望能够返回路径(不知道还有什么要调用它)到点击元素。 alert()现在会做。

I want to be able return the path (don't know what else to call it) to the clicked element. alert() will do for now.

可以说我点击了第二个div中的第二个li元素。我想要一个叫的电话:

Lets say I clicked on the second li element in the second div. I would like a call back of:

div:eq(1) li:eq(1)

如果我点击第一个div的第一个li元素,它会是:

If I clicked on the first li element of the first div, it would be:

div:eq(0) li:eq(0)

我会如何做到这一点?

是否有插件可以做到这一点,或者我需要从头开始获得路径中元素的索引?

Is there a plugin out that can do this, or would I need to make it from scratch to get the index of element in the path?

感谢:)

推荐答案

<

var q = $(this) .parentsUntil('body') .andSelf() .map(function() { return this.nodeName + ':eq(' + $(this).index() + ')'; }).get().join('>');

受这个答案。

更多推荐

返回元素的完整路径?

本文发布于:2023-11-30 05:34:02,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   元素   完整

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!