2014-12-06 74 views
0

我需要在创造IE 11.一个TextRange在以前的IE一点帮助有码的createTextRange在IE11

var theSelection = document.selection.createRange(); 
--Code that works on a text range because create range returns a text range 

我明白,IE11不再支持selection这一点,我们必须使用getSelection。所以,现在我的新的代码看起来像这样

var theSelection = document.getSelection().createRange(); 
--Code that works on a text range because create range returns a text range 

是我遇到的问题是,我不断收到这个错误,它说的是对象不支持方法或属性createRange。我也尝试过这样,这给了我同样的错误。

var theSelection = document.getSelection().toString().createRange(); 
--Code that works on a text range because create range returns a text range 

任何想法?

+1

请尝试使用'document.body.createTextRange()'代替。 – PHPglue 2014-12-06 00:15:40

+0

@PHPglue的作品,但它不允许我得到选定的文本。 – 2014-12-06 00:43:17

回答

0

其实,我相信你有你需要什么...

document.selection.createRange()被替换document.getSelection() ...

还有一张纸条here那......

document.selection是替换为window.getSelection ...

+0

我检查,当我做一个'console.log(document.getSelection())'我得到一个选择对象返回而不是文本范围对象。此外,当我尝试执行查找功能它说该属性或方法不支持.http://msdn.microsoft.com/en-us/library/ie/ms535872(v = vs.85).aspx – 2014-12-06 00:50:30

+0

是你在寻找findText方法吗? (http://msdn.microsoft.com/en-us/library/ie/ms536422(v=vs.85).aspx) – 2014-12-07 00:31:57

+0

@LanceLeonard我实际上已经在使用'findText'方法。但为了让我调用thet方法,我需要从新的'getSelection'函数创建一个textrange,这是我遇到问题的地方。 – 2014-12-08 17:24:19