Created
May 20, 2015 19:17
-
-
Save anonymous/d639cab52292296ba350 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/qulurapewu
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-git.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div></div><div><div></div><div></div><div></div><div></div><div></div><div></div><div class='fff'></div></div><div></div><div></div><div></div> | |
<script id="jsbin-javascript"> | |
$(document).ready(function(){ | |
/****/ | |
jQuery.fn.extend({ | |
getPath: function() { | |
var pathes = []; | |
this.each(function(index, element) { | |
var path, $node = jQuery(element); | |
while ($node.length) { | |
var realNode = $node.get(0), name = realNode.localName; | |
if (!name) { break; } | |
name = name.toLowerCase(); | |
var parent = $node.parent(); | |
var sameTagSiblings = parent.children(name); | |
if (sameTagSiblings.length > 1) | |
{ | |
allSiblings = parent.children(); | |
index = allSiblings.index(realNode) +1; | |
if (index > 0) { | |
name += ':nth-child(' + index + ')'; | |
} | |
} | |
path = name + (path ? ' > ' + path : ''); | |
$node = parent; | |
} | |
pathes.push(path); | |
}); | |
return pathes.join(','); | |
} | |
}); | |
/***/ | |
console.log(jQuery('.fff').first().getPath()); | |
/***/ | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">$(document).ready(function(){ | |
/****/ | |
jQuery.fn.extend({ | |
getPath: function() { | |
var pathes = []; | |
this.each(function(index, element) { | |
var path, $node = jQuery(element); | |
while ($node.length) { | |
var realNode = $node.get(0), name = realNode.localName; | |
if (!name) { break; } | |
name = name.toLowerCase(); | |
var parent = $node.parent(); | |
var sameTagSiblings = parent.children(name); | |
if (sameTagSiblings.length > 1) | |
{ | |
allSiblings = parent.children(); | |
index = allSiblings.index(realNode) +1; | |
if (index > 0) { | |
name += ':nth-child(' + index + ')'; | |
} | |
} | |
path = name + (path ? ' > ' + path : ''); | |
$node = parent; | |
} | |
pathes.push(path); | |
}); | |
return pathes.join(','); | |
} | |
}); | |
/***/ | |
console.log(jQuery('.fff').first().getPath()); | |
/***/ | |
});</script></body> | |
</html> |
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
$(document).ready(function(){ | |
/****/ | |
jQuery.fn.extend({ | |
getPath: function() { | |
var pathes = []; | |
this.each(function(index, element) { | |
var path, $node = jQuery(element); | |
while ($node.length) { | |
var realNode = $node.get(0), name = realNode.localName; | |
if (!name) { break; } | |
name = name.toLowerCase(); | |
var parent = $node.parent(); | |
var sameTagSiblings = parent.children(name); | |
if (sameTagSiblings.length > 1) | |
{ | |
allSiblings = parent.children(); | |
index = allSiblings.index(realNode) +1; | |
if (index > 0) { | |
name += ':nth-child(' + index + ')'; | |
} | |
} | |
path = name + (path ? ' > ' + path : ''); | |
$node = parent; | |
} | |
pathes.push(path); | |
}); | |
return pathes.join(','); | |
} | |
}); | |
/***/ | |
console.log(jQuery('.fff').first().getPath()); | |
/***/ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jsbin create it anonymously