页面: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
我以为那个页面覆盖了document.evaluate,但它显示了
document.evaluate 函数计算(){本机代码}
delete document.evaluate没有帮助,那么还能有什么东西破坏document.evaluate呢?
发布于 2013-10-02 21:32:39
您在问题中显示的页面使用xhtml命名空间,您看到的其他页面也可能使用xhtml命名空间。
由于您正在为命名空间解析器参数设置null,所以它找不到元素。
注意: XPath定义没有前缀的QNames只匹配空命名空间中的元素。在XPath中,找不到应用于常规元素引用的默认名称空间(例如,xmlns=‘http://www.w3.org/1999/xhtml’的p@id='_myid‘)。要匹配非空命名空间中的默认元素,您必须使用表单(如我的‘ )引用特定元素,或者使用前缀名称测试,并创建将前缀映射到命名空间的命名空间解析器。
因此,如果设置了解析器,则可以通过前缀正确访问元素:
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).singleNodeValuehttps://stackoverflow.com/questions/19146056
复制相似问题