XPath迭代异常:“试图使用一个不可用或不再可用的对象”; [英] XPath iteration Exception: "An attempt was made to use an object that is not, or is no longer, usable"

查看:243
本文介绍了XPath迭代异常:“试图使用一个不可用或不再可用的对象”;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Mozilla的在JavaScript中使用XPath的简介相比,代码段略有更改抛出异常。

Slightly changed snippet from Mozilla's Introduction to using XPath in JavaScript is throwing exception.

[Exception ...试图使用一个不可用或不再可用的对象代码: 11 nsresult: 0x8053000b(InvalidStateError)位置:调试器评估代码行:6]

代码:

var headings = document.evaluate('//h2', document, null, XPathResult.ANY_TYPE, null );
var thisHeading = headings.iterateNext();

while (thisHeading) {
  thisHeading.textContent = '\n';
  thisHeading = headings.iterateNext();
}

与原始代码的区别在于我正在更改元素-而不是

The difference from original piece of code is that I'm altering elements - not just reading them.

为什么我看到异常以及如何在迭代中更改元素?

Why do I see the exception and how to alter elements in iteration?

推荐答案

我认为,从xpath和javascript开始时,这是一个普遍的问题,对于所有急于阅读完整内容的人来说简介/文档,因此应该在SO上。

I think it's common problem when starting with xpath and javascript and for everyone who is in rush and don't read whole introduction/documentation, so it should be on SO.

headings.resultType 4 UNORDERED_NODE_ITERATOR_TYPE


当XPath表达式的结果是节点集时,这是默认的返回类型。

This is the default return type when the result of the XPath expression is a node set.

您必须知道它是哪个 resultType ,因为:

You have to know which resultType it is, because:


XPathResult对象允许以三种主要的不同类型返回节点集:

XPathResult object allows node-sets to be returned in 3 principal different types:


  • 迭代器

  • 快照

  • 第一个节点

最重要的信息是:当节点集是 * _ ITERATOR_TYPE 之一时,您不能更改DOM。

And the most important information is: you cannot change DOM when node-set is one of the*_ITERATOR_TYPE.


..如果在两次迭代之间对文档进行了突变(修改了文档树),则该迭代将无效,并且XPathResult的invalidIteratorState属性设置为true,并引发NS_ERROR_DOM_INVALID_STATE_ERR异常。

..if the document is mutated (the document tree is modified) between iterations that will invalidate the iteration and the invalidIteratorState property of XPathResult is set to true, and a NS_ERROR_DOM_INVALID_STATE_ERR exception is thrown.

因此,如果要更改某些元素,则必须专门要求快照之一 resultType 。您有两个选择:

So if you want to alter some elements you have to specifically ask for one of the Snapshots resultType. You have two options:


  • UNORDERED_NODE_SNAPSHOT_TYPE

  • ORDERED_NODE_SNAPSHOT_TYPE

在您的情况下,这就是您想要的:

In your case this is what you want:

var headings = document.evaluate('//h2', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for ( var i=0 ; i < headings.snapshotLength; i++ ) {
   headings.snapshotItem(i).textContent = '\n';
}

请注意您要更改的内容:

And be aware of what you are changing:


快照不会随文档突变而改变,因此与迭代器不同,快照不会变为无效,但它可能与当前文档不对应,例如,节点可能具有被移动,它可能包含不再存在的节点,或者可能已经添加了新节点。

Snapshots do not change with document mutations, so unlike the iterators the snapshot does not become invalid, but it may not correspond to the current document, for example the nodes may have been moved, it might contain nodes that no longer exist, or new nodes could have been added.

这篇关于XPath迭代异常:“试图使用一个不可用或不再可用的对象”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆