从IE11中的选择创建TextSelection [英] Create TextSelection from selection in IE11
问题描述
我正在尝试在IE11中修复一个应用程序而且我遇到了这个错误:在旧版本的IE中,这是一个简单的选择对象但在IE11中已弃用。 MSDN页面提供使用getSelection而不是它,但它不一样。我需要创建一个 TextRange 根据选择和旧API有一个简单的解决方案:
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 offer to use getSelection instead of that, but it is not the same. I need to create a TextRange based on the selection and in the old API there was a simple solution:
// there is a TextRange object what I need
var textRange = document.selection.createRange();
新 HTMLSelection 对象没有createRange()方法,我没有找到任何正确的解决方案,什么不修改DOM。
The new HTMLSelection object has no createRange() method and I didn't find any proper solution, what is not modify the DOM.
我试试这个: http://jsfiddle.net / p4Lu4 / 1 / (用法:选择一些文字然后点击任意键。)
I try this: http://jsfiddle.net/p4Lu4/1/ (Usage: Select some text and hit any key. )
问题:如果选择抛出其中一个红色框,它会删除该框。
The problem with it: If you select throw one of the red boxes, it will remove the box.
任何人都可以知道更好的解决方案吗?
Can anyone know a better solution?
解决方案:
我想,我找到了一个解决方案。不是最好也最好但它对我有用:我可以从Selection中创建完全相同的TextRange。
I think, I found a solution. Not the best and nicest but it is working for me: I can create the exact same TextRange from Selection.
更新2:
我为此创建了一个模块: https ://gist.github.com/festo/50fe800c7369db140a62
I create a modul for this: https://gist.github.com/festo/50fe800c7369db140a62
推荐答案
我的 Rangy 库有代码将DOM兼容范围转换为IE TextRange 作为在IE< = 8中提供DOM范围和选择支持的一部分。最新版本公开了此转换显式选择对象:
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();
请注意 getNativeTextRange()
方法一个Rangy选择只存在于IE中。
Note that the getNativeTextRange()
method of a Rangy selection only exists in IE.
另一个选择是使用Rangy的 TextRange模块,其中包括向Rangy的范围对象添加类似IE的 findText()
方法,以及类应用程序模块进行突出显示。这适用于所有主流浏览器。
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.
演示: 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.
这篇关于从IE11中的选择创建TextSelection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!