Created
March 28, 2014 11:32
-
-
Save SeavantUUz/9830668 to your computer and use it in GitHub Desktop.
This file contains 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 getPathTo(element) { | |
if (element.id!=='') | |
return 'id("'+element.id+'")'; | |
if (element===document.body) | |
return element.tagName; | |
var ix= 0; | |
var siblings= element.parentNode.childNodes; | |
for (var i= 0; i<siblings.length; i++) { | |
var sibling= siblings[i]; | |
if (sibling===element) | |
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']'; | |
if (sibling.nodeType===1 && sibling.tagName===element.tagName) | |
ix++; | |
} | |
} | |
var getElementByXpath = function (path) { | |
return document.evaluate(path, document, null, 9, null).singleNodeValue; | |
}; | |
$(function(){ | |
$('*').bind('click',function(event){ | |
var x = event.pageX - window.pageXOffset; | |
var y = event.pageY - window.pageYOffset; | |
element = document.elementFromPoint(x,y); | |
var path = getPathTo(element); | |
console.log(path); | |
var _element = getElementByXpath(path); | |
console.log(_element); | |
event.stopPropagation(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment