Javascript document.evaluate with XPathResult.FIRST_ORDERED_NODE_TYPE返回未定义
Firebug将xpath结果打印为未定义,但它不是未定义的Javascript document.evaluate with XPathResult.FIRST_ORDERED_NODE_TYPE返回未定义,javascript,xpath,firebug,Javascript,Xpath,Firebug,Firebug将xpath结果打印为未定义,但它不是未定义的 function xpathTest() { var div1 = document.getElementById("div1"); var result = document.evaluate("//div[text()='Hello']", div1, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); console.log(result)
function xpathTest()
{
var div1 = document.getElementById("div1");
var result = document.evaluate("//div[text()='Hello']", div1, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null);
console.log(result); // Firebug prints undefined
console.log(result === undefined); // prints false
console.log(typeof result); // prints object
console.log(result.singleNodeValue); // prints Hello
}
此处为Html:
<body onload="xpathTest()">
<div id="div1">
<div>Hello</div>
</div>
</body>
你好
所以xpath结果的实际toString()(?)实现是不正确的,还是Firebug的错?似乎这是Firebug的问题
var resultDiv = document.getElementById("resultDiv");
add("Result: " + result); // Result: [object XPathResult]
add("(result === undefined): " + (result === undefined));
// (result === undefined): false
add("(typeof result): " + (typeof result));
// (typeof result): object
add("result.singleNodeValue: " + result.singleNodeValue);
// result.singleNodeValue: [object HTMLDivElement]
function add(content)
{
resultDiv.innerHTML += content + "<br>";
}
var resultDiv=document.getElementById(“resultDiv”);
添加(“结果:+结果);//结果:[对象XPathResult]
添加(“(结果===未定义):”+(结果===未定义));
//(结果===未定义):false
添加(“(结果类型):”+(结果类型));
//(结果类型):对象
添加(“result.singleNodeValue:+result.singleNodeValue”);
//result.singleNodeValue:[对象HtmlLevel]
功能添加(内容)
{
resultDiv.innerHTML+=content+“
”;
}
请参阅使用Chrome,firefox内置的web控制台
result
显示为XPathResult
,在firebug中仅显示未定义的,因此可能是firebug问题。但是,使用console.dir会使firebug打印出XPathResultobject@PatrickEvans知道了。非常感谢。