我是XPath的新手,所以我一直在浏览器中试验它。我一直在尝试的一种查询形式是基于文本内容匹配节点,我一直在使用以下函数的变体:
function getDOMNodesFromText = function ( text, parentNode ) {
if ( !parentNode ) parentNode = document.body;
return(
document.evaluate(
"//*[contains(translate(text(), \"'’\", ''), '" + text.trim().replace(/['’]/g, '') + "')]",
parentNode,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
)
);
}
我在浏览器控制台中将此函数定义放在Grantland article中,并且在大多数情况下,我能够按预期找到节点。但是,有些节点在任何情况下我都不能使用此函数匹配任何文本,或者使用此函数的变体。
示例节点是表单的段落标记中的最后一个文本节点:
<p>
Everyone was buzzing about it. The Thunder under Sam Presti have long had an affection for Lopez; one former Thunder official
<a href="https://www.youtube.com/watch?v=YDqdEiQm2XU" target="_blank">
revealed to me two years ago
</a>
that the franchise thought seriously about drafting Lopez over Russell Westbrook in 2008. The Thunder were on the verge of acquiring Lopez at a super-cheap price — something like Jeremy Lamb, Grant Jerrett, and Kendrick Perkins’s expiring contract — until the Nets wisely pulled back to further test the market. I’d expect the Thunder to inquire on Lopez again soon.
</p>
(这是div.blog-body
- document.querySelectorAll( '#layout-main .blog-body p' )[8]
中的第八段标记,用于在控制台中快速抓取。)
我在文章中发现了几个其他文本节点,我遇到了同样的问题 - 无法使用上面的函数从文本内容中选择它们。我找到的所有示例都是文本节点,其父元素包含其他节点(在所有情况下,我发现,无法访问的文本节点在a
标记内跟有p
标记,但是这种形式有反例在同一篇文章中)。否则我没有看到任何共性。我尝试了许多不同的父节点,从body
到直接父节点,没有成功。以上段落标记的示例:
getDOMNodesFromText( "I’d expect the Thunder to inquire on Lopez again soon.", document.body )
返回一个空的XPathResult
对象(snapshotLength为0)。
我错过了什么?