简体   繁体   中英

Find text node after a node with XPath

I am trying to use document.evaluate to find text nodes matching my query. The issue I found is that with the XPath I am using, it is not finding text nodes that follow another element. For example, given these conditions ( jsFiddle ):

HTML

<div><p>$100</p></div>
<p>Test test <span>test</span> $100 test</p>

Javascript

var item,
  indx = 0,
  items = document.evaluate('//*[contains(text(),"$")]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

while (item = items.snapshotItem(indx++)) {
  if (item.nodeName !== 'SCRIPT') {
    console.log(item.textContent);
  }
}

This XPath only finds the first $ , the second isn't found because of the <span> in the middle.

So my question is how can I change the javascript (not the HTML) to find both matches?

将XPath表达式更改为//*[text()[contains(.,"$")]]因为这样会选择具有包含$符号的文本子节点的元素。

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