Returning the full path to an element?

后端 未结 4 1706
梦毁少年i
梦毁少年i 2020-12-31 20:31

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

For example, lets say I have this HTML code:

  • i
4条回答
  •  生来不讨喜
    2020-12-31 21:07

    This is how you can reconstruct the full path:

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

    Inspired by this answer.

提交回复
热议问题