我正在寻找一种方法来查找点击时元素的完整路径。
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('>');受这个答案。