Create TextSelection from selection in IE11

前端 未结 1 1401
忘掉有多难
忘掉有多难 2021-01-03 06:07

I am trying to fix an application in IE11 and I stuck in this bug: In the older version of IE ther was a simple selection object but it is deprecated in IE11. The MSDN page

相关标签:
1条回答
  • 2021-01-03 06:40

    My Rangy library has code to convert a DOM-compatible range to an IE TextRange as part of providing DOM range and selection support in IE <= 8. The latest build exposes this conversion explicitly on the selection object:

    var textRange = rangy.getSelection().getNativeTextRange();
    

    Note that the getNativeTextRange() method of a Rangy selection only exists in IE.

    Another alternative is to use Rangy's TextRange module, which among other things adds an IE-like findText() method to Rangy's range object, along with the class applier module to do the highlighting. This will work in all major browsers.

    Demo: http://jsfiddle.net/sycqeev2/

    I'm not sure what's supposed to happen in your demo when you highlight text and press a key, so I haven't attempted to do anything with that.

    0 讨论(0)
提交回复
热议问题