[removed] error 800a025e using range selector

后端 未结 3 1535
栀梦
栀梦 2021-01-07 22:36

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

Here a screenshot from ie11 dev tool

3条回答
  •  渐次进展
    2021-01-07 23:16

    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.

    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:

          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.

提交回复
热议问题