在 Internet Explorer 中设置文本区域选择 [英] Set textarea selection in Internet Explorer
问题描述
我正在寻找一种方法来在 Internet Explorer 的文本区域中设置一个选择.在其他浏览器中,这工作得很好:
I'm looking for a way to set a selection in a textarea in Internet Explorer. In other browsers, this works just fine:
textarea.selectionStart = start;
textarea.selectionEnd = end;
在 IE 中,我假设我必须使用 createRange
并以某种方式调整选择,但我不知道如何.
In IE, I assume I have to use createRange
and adjust the selection somehow, but I cannot figure out how.
链接到有关 createRange
和相关方法的适当文档的额外奖励积分,MSDN 并没有多大帮助.
Extra bonus points for a link to a proper documentation about createRange
and associated methods, MSDN isn't helping out much.
推荐答案
这对我有用:
<textarea id="lol">
noasdfkvbsdobfbgvobosdobfbgoasopdobfgbooaodfgh
</textarea>
<script>
var range = document.getElementById('lol').createTextRange();
range.collapse(true);
range.moveStart('character', 5);
range.moveEnd('character', 10);
range.select();
</script>
有用的链接:
- http://help.dottoro.com/ljlwflaq.php
- http://www.webreference.com/programming/javascript/ncz/
- http://www.quirksmode.org/dom/range_intro.html
moveStart() 在 MSDN:http://msdn.microsoft.com/en-us/library/ms536623%28VS.85%29.aspx
moveStart() at MSDN: http://msdn.microsoft.com/en-us/library/ms536623%28VS.85%29.aspx
moveEnd() 在 MSDN:http://msdn.microsoft.com/en-us/library/ms536620%28VS.85%29.aspx
moveEnd() at MSDN: http://msdn.microsoft.com/en-us/library/ms536620%28VS.85%29.aspx
这篇关于在 Internet Explorer 中设置文本区域选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!