page: http://h4z.it/View/-20100729_designtea.jpg
要在控制台中执行的代码: document.evaluate("//img",document,null,9,null).singleNodeValue
或
document.evaluate("//a",document,null,9,null).singleNodeValue
甚至
document.evaluate("//html",document,null,9,null).singleNodeValue
结果(使用Chrome和Firefox测试(:null
我以为该页面覆盖了文档。评估,但它显示
文档.评估
函数 evaluate(( { [本机代码] }
delete document.evaluate
也无济于事,那么还能是什么打破document.evaluate
呢?
您在问题中显示的页面使用 xhtml 命名空间,您可能看到这种情况发生的其他页面也是如此。
由于您正在为命名空间解析器参数设置null
,因此它找不到元素。
MDN 使用 XPath
注意:XPath 定义了不带前缀的 QNames,以仅匹配 空命名空间。XPath 中无法获取默认值 应用于常规元素引用的命名空间(例如, p[@id='_myid'] for xmlns='http://www.w3.org/1999/xhtml'(.要匹配 非 null 命名空间中的默认元素,您必须引用 使用表单的特定元素,例如 ['namespace-uri((='http://www.w3.org/1999/xhtml' and name((='p' 和 @id='_myid'](此方法适用于动态 XPath,其中 命名空间可能未知(或使用前缀名称测试,并创建 将前缀映射到命名空间的命名空间解析程序。
因此,如果您设置了解析器,则可以通过为它们添加前缀来正确访问该元素:
function resolver(prefix){
return prefix === "xhtml" ? 'http://www.w3.org/1999/xhtml' : null;
}
document.evaluate("//xhtml:a",document,resolver,9,null).singleNodeValue
.评估文档
创建解析程序
如果要获取节点而不必知道命名空间local-name()
则可以使用 XPath 函数作为表达式的一部分
document.evaluate("//*[local-name()='img']",document,null,9,null).singleNodeValue