Iterate over framesets with XPath expression in JavaScript?

对着背影说爱祢 提交于 2019-12-12 04:25:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!