admin管理员组文章数量:1334342
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?
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?
- Why not just store a reference to that element in a variable within scope of all the required functions? – Rory McCrossan Commented Apr 6, 2016 at 13:20
-
getPathTo(jQuery('h3').first()[0])
– epascarello Commented Apr 6, 2016 at 13:21 - I Think your Problem is that you use a documentElement in jQuery Context. You can try to extract the element to javascript native level. Like this: var element = jQuery('h3'); element.childNodes.... – Denis Kohl Commented Apr 6, 2016 at 13:27
- @RoryMcCrossan because I need the element to be retrieved in another session. – fsinisi90 Commented Apr 6, 2016 at 13:27
- @epascarello Thanks, it works. Maybe you want to post it like an answer. – fsinisi90 Commented Apr 6, 2016 at 13:27
1 Answer
Reset to default 5The 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))
本文标签: javascriptGet XPath of a DOM element with jQueryStack Overflow
版权声明:本文标题:javascript - Get XPath of a DOM element with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742370036a2462054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论