1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Scriptsrc= "/jquery/jquery-1.11.1.min.js">5 </Script>6 <Script>7 8 functionReadxpath (Element) {9 if(Element.id! == ""){//determines the id attribute, if the element has an ID, displays//*[@id = "XPath"] form contentTen return '//*[@id =\ "'+element.id+'\"]'; One } A - if(Element.getattribute ("class")! == NULL){ //determines the class attribute, if the element has a class, displays//*[@class = "XPath"] form content - return '//*[@class =\ "'+Element.getattribute ("class")+'\"]'; the } - //because the XPath attribute has more than one ID and class, you can add attributes in a more class form - - //The main string translation issues need to be needed here + - + if(Element== document.body) {//recursion to body, end recursion A return '/html/'+element.tagName.toLowerCase (); at } - - varIX= 0,//position in NodeList, and initialize each click - siblings=Element.parentNode.childNodes;//child elements of a sibling - in for (varI= 0, L=siblings.length; I<l; I++) { - varSibling=Siblings[i]; to if(Sibling== Element) {//If this element is an element in the siblings array, the recursive operation is performed + returnArguments.callee (Element.parentnode)+'/'+element.tagName.toLowerCase ()+(ix+1)==1?"':'['+(ix+1)+']');//Ix+1 is because the XPath is counted starting from 1, element.tagname+ ((ix+1) ==1? ': ' [' + (ix+1) + '] ') ternary operator, if the first one is not displayed, starting from 2 displays - }Else if(Sibling.nodetype== 1 &&Sibling.tagname== element.tagname) {//If it is not, determine if it is an element and whether it is the same element, and if it is the same, start accumulating the IX++; * } $ }Panax Notoginseng }; - the $ (document). Ready (function () { + varXPath= "', O; A $('*'). Click (function(e) { the e.stoppropagation ();//Stop bubbling + o= This; - alert (Readxpath (o)); $ }); $ }); - - </Script> the </Head> - <Body>Wuyi <P>If you click on me, I will disappear.</P> the <P>Click on me and I will disappear.</P> - <P>Also to click on Me Oh.</P> Wu </Body> - </HTML>
Gets the XPath path of the HTML element