返回元素的完整路径? [英] Returning the full path to an element?

查看:101
本文介绍了返回元素的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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('>');

这个答案

这篇关于返回元素的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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