Javascript:使用范围选择器时出现错误 800a025e

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22914075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:01:11  来源:igfitidea点击:

Javascript: error 800a025e using range selector

javascriptinternet-explorer-11

提问by realtebo

I'm executing these simple rows of javascript in latest ie 11, just to select all content of a div

我在最新的 ie 11 中执行这些简单的 javascript 行,只是为了选择 div 的所有内容

Here a screenshot from ie11 dev tool

这是 ie11 开发工具的截图

Code and error - screenshot

代码和错误 - 屏幕截图

Like you can see, IE alter me saying: "unable to complete the operation due to error 800a025e".

如您所见,IE 更改我说:“由于错误 800a025e,无法完成操作”。

I'm not able to understand the nature, the source, of the problem, and no others stack overflow questions I read give me a clear answer.

我无法理解问题的性质、来源,而且我读过的其他堆栈溢出问题也没有给我一个明确的答案。

This is full code of my selectText jQuery "personal" extension

这是我的 selectText jQuery“个人”扩展的完整代码

jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    // console.log(this, element);
    if (typeof element == 'undefined') {
        return;
    }
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

Element in this case is a

这种情况下的元素是

[Object HTMLTableElement]

Edit 1: with a little mod:

编辑 1: 有一个小模组:

var range = document.body.createTextRange();
var retval = range.moveToElementText(element);
console.log (retval);
range.select();

I'm able to say you retval is undefined. This code work on ff without problems, so element selector is fine. jQuery version is 1.11

我可以说你 retval 是未定义的。这段代码在 ff 上运行没有问题,所以元素选择器很好。jQuery 版本是 1.11

采纳答案by realtebo

I resolved using this trick:

我使用这个技巧解决了:

I wrapped the html table with a DIV

我用 DIV 包裹了 html 表

I use the same code, as is, with no changes, against the DIV, instead of using for selecting only the table.

我对 DIV 使用相同的代码,没有任何更改,而不是仅用于选择表。

It's working.

它正在工作。

Probably html table is not a selectable text for IE

可能 html 表不是 IE 的可选文本

回答by kamelkev

None of the above suggestions or hints properly resolves this particular problem.

上述建议或提示均未正确解决此特定问题。

The issue itself is caused by non-text (for example table) elements getting collected together with other elements within a TextRange collection. Attempting to clear the collection while these table elements remain within the collection will unconditionally cause the reported error above.

问题本身是由非文本(例如表格)元素与 TextRange 集合中的其他元素一起收集引起的。在这些表元素保留在集合中时尝试清除集合将无条件地导致上述报告的错误。

The issue affects IE 9/10/11, and there are specific workarounds for each of those browsers. Unfortunately those workarounds are not compatible with other browsers (firefox, safari, chrome, edge), so we need to special case the clear handling for IE 9/10/11 while providing alternate handling for more standards compliant browsers. The following appears to work everywhere, at least as of this writing:

该问题会影响 IE 9/10/11,并且每个浏览器都有特定的解决方法。不幸的是,这些解决方法与其他浏览器(firefox、safari、chrome、edge)不兼容,因此我们需要对 IE 9/10/11 进行特殊处理,同时为更多符合标准的浏览器提供替代处理。以下似乎适用于任何地方,至少在撰写本文时:

      if (doc.body.createTextRange) { // All IE but Edge
        var range = doc.body.createTextRange();
        range.collapse();
        range.select();
      }
      else {
        doc.getSelection().removeAllRanges();
      }

The IE specific branch is a roundabout way of clearing a range containing table elements without actually invoking the empty() method which otherwise results in the troublesome exception.

IE 特定分支是清除包含表元素的范围的一种迂回方式,而无需实际调用 empty() 方法,否则会导致麻烦的异常。

回答by Dan Dascalescu

I got this error when trying to window.getSelection().removeAllRanges();and there was no selection. One workaround is to check if there is a selection first:

我在尝试时遇到此错误window.getSelection().removeAllRanges();并且没有选择。一种解决方法是先检查是否有选择:

if (window.getSelection().getRangeAt(0).getClientRects.length > 0) {
    window.getSelection().removeAllRanges();
}

Also, this question is a duplicate of Could not complete the operation due to error 800a025e, but I couldn't mark it as such because the other question doesn't have an accepted answers.

此外,这个问题是由于错误 800a025e 无法完成操作的重复,但我无法将其标记为这样,因为另一个问题没有被接受的答案。