问题描述
我正在寻找一种方法来设置在Internet Explorer中的textarea中的选择。在其他浏览器中,这很好用:
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
在MSDN上的moveStart():
moveStart() at MSDN: http://msdn.microsoft.com/en-us/library/ms536623%28VS.85%29.aspx
在MSDN上的moveEnd():
moveEnd() at MSDN: http://msdn.microsoft.com/en-us/library/ms536620%28VS.85%29.aspx
这篇关于在Internet Explorer中设置textarea选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!