这个xpath表达式出了什么问题?

时间:2012-02-03 16:04:18

标签: javascript xpath

http://jsbin.com/eyeqer/2/edit

var x = document.evaluate('//p/following-sibling::text()', document, null, XPathResult.ANY_TYPE, null);

var theTitle = x.iterateNext();
while (theTitle){
alert(theTitle.textContent);
theTitle = x.iterateNext();
}

<body>
  <p>Hello</p>
  <p>World</p>
  <p>HW</p>

</body>

我希望看到“世界”和“硬币”,但它没有用!为什么?我该如何解决?

1 个答案:

答案 0 :(得分:3)

您需要选择节点,因此您应该使用*,而不是text()

var x = document.evaluate('//p/following-sibling::*', document, null, XPathResult.ANY_TYPE, null);