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); // 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>所以实际的toString()(?)xpath结果的实现不正确还是Firebug的错误?
发布于 2014-12-24 15:28:51
这似乎是一个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>";
}请参阅JS Fiddle
https://stackoverflow.com/questions/27632921
复制相似问题