在Internet Explorer中设置textarea选择 [英] Set textarea selection in Internet Explorer

查看:112
本文介绍了在Internet Explorer中设置textarea选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来设置在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(): 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

在MSDN上的moveEnd(): 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中设置textarea选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆