Created
April 7, 2013 13:44
-
-
Save Gufran/5330550 to your computer and use it in GitHub Desktop.
[jQuery] Trace path to a node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ){ | |
jQuery.fn.trace = function () { | |
if (this.length != 1) | |
return ''; | |
var path, node = this; | |
while (node.length) { | |
var realNode = node[0], name = realNode.localName; | |
if (!name) break; | |
name = name.toLowerCase(); | |
if (realNode.id) { | |
return name + '#' + realNode.id + (path ? '>' + path : ''); | |
} else if (realNode.className) { | |
name += '.' + realNode.className.split(/\s+/).join('.'); | |
} | |
var parent = node.parent(), siblings = parent.children(name); | |
if (siblings.length > 1) name += ':eq(' + siblings.index(node) + ')'; | |
path = name + (path ? '>' + path : ''); | |
node = parent; | |
} | |
return path; | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment