Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript document.evaluate allways在某些页面\站点的singleNodeValue中返回null_Javascript_Google Chrome_Firefox_Document.evaluate - Fatal编程技术网

Javascript document.evaluate allways在某些页面\站点的singleNodeValue中返回null

Javascript document.evaluate allways在某些页面\站点的singleNodeValue中返回null,javascript,google-chrome,firefox,document.evaluate,Javascript,Google Chrome,Firefox,Document.evaluate,第页: 要在控制台中执行的代码: 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(“//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,但它显示

文件。评估

函数求值(){[本机代码]}


删除文档。evaluate
没有帮助,那么还有什么会破坏
文档。evaluate

您在问题中显示的页面使用xhtml名称空间,您看到的其他页面也可能使用xhtml名称空间

因为您正在为名称空间解析器参数设置
null
,所以它找不到元素

注意:XPath定义了不带前缀的QNames,以仅匹配 空名称空间。在XPath中,无法获取默认值 应用于常规元素引用的命名空间(例如。, 对于xmlns='',p[@id=''我的id']。相配 非空命名空间中的默认元素,您必须 使用以下形式的特定元素: ['namespace-uri()=''和name()='p'和 @id=''u 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

在不声明解析器函数的情况下,可以实现这一点吗?像
document.evaluate(“//xhtml”,document,”http://www.w3.org/1999/xhtmlsingleNodeValue
evaluate期望解析器参数是函数,因此您可以使用匿名函数,
function(){return'blahblahblah';}
作为参数,还编辑了答案,以演示如何在不知道名称空间的情况下生成表达式