简体   繁体   中英

Get XPath of a DOM element with jQuery

I need to get the XPath of a DOM element to persist it so I can look for that element lather.

I've tried the getPathTo method of this answer but when I call the method with a jQuery-created object like this...

getPathTo(jQuery('h3').first());

...I get this error:

Uncaught TypeError: Cannot read property 'childNodes' of undefined(…)

I've tried to replace parentNode with parent() , childNodes with children() , and tagName with prop('tagName') , but then I received undefined as the function result...

So, do you have a similar function to getPathTo that works with jQuery?

The method expects a DOM node and you are giving it a jQuery object

getPathTo(jQuery('h3').first()[0])

or

getPathTo(jQuery('h3').first().get(0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM