xpath - 将搜索限制为节点不起作用?
我在这里做错了什么?我试图将 xpath 搜索限制到表中的特定行,但我的查询始终返回第一行中跨度的内容:
var query = "//span[contains(@id, 'timer')]";
var root = document.getElementById('movements').rows[1];
document.evaluate(query, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent
请帮忙!
What am I doing wrong here? I am trying to limit my xpath search to a specific row in my table but my query always returns the content of the span in the first row:
var query = "//span[contains(@id, 'timer')]";
var root = document.getElementById('movements').rows[1];
document.evaluate(query, root, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题:这是一个绝对 XPath 表达式,它忽略了您从特定行对其求值的事实。
解决方案:要达到想要的结果,请使用:
.//span[contains(@id, 'timer')]
The problem: This is an absolute XPath expression and it ignores the fact that you are evaluating it from a specific row.
Solution: To achieve the wanted result use:
.//span[contains(@id, 'timer')]
您将获得第一行:
这将获得第二行:
您需要指定所需行的正确索引。
You are getting the first row:
This will get the second row:
You need to specify the correct index of the row you want.
试试这个:
Try this instead: