问题
Why does the following JavaScript, when run in Firefox 3.6.3, delete all FRAMESET
elements in a document, but the similar script that instead uses an XPath expression to select the FRAMESET
elements, does not? Is document.evaluate()
simply unable to match FRAMESET
elements? Is there an error in the XPath expression? Is there some other error?
Select all FRAMESET
elements using method document.getElementsByTagName()
(succeeds):
var framesets = document.getElementsByTagName('frameset');
for (var i = 0; i < framesets.length; i++) {
framesets[i].parentNode.removeChild(framesets[i]);
}
Select all FRAMESET
elements using an XPath expression (fails):
var framesets = document.evaluate("//frameset", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < framesets.length; i++) {
framesets[i].parentNode.removeChild(framesets[i]);
}
回答1:
It appears that the XPathResult is iterator-ish. It does not have a length property.
https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate
framesets.length
will be undefined. In short, Here Be Dragons.
来源:https://stackoverflow.com/questions/2791706/iterate-over-framesets-with-xpath-expression-in-javascript