nat*_*e75 5 javascript internet-explorer cursor selection textinput
我已经在 Firefox、Safari 和 Chrome 中使用了它。
我希望能够以编程方式在 Internet Explorer 的 INPUT 字段中设置文本光标的位置。
我在各种网站上查找了这个主题,通常发现了相同的技术:
var el = document.getElementById("myTextField");
var pos = 6;
if (document.selection) {
el.focus();
var selection = document.selection.createRange();
selection.moveStart("character", -el.value.length);
selection.moveStart("character", pos);
selection.moveEnd("character", 0);
selection.select();
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试执行此操作时,无论我提供什么位置,光标始终会移动到值的末尾。
我是否误解了人们一直在使用的技术?我在某处错过了什么吗?这有点令人沮丧,但这当然是使用这些不同浏览器进行 Web 开发的本质。
在此先感谢您的帮助。
以下代码在 IE 9 中对我有用
<script type="text/javascript">
var input = document.getElementById("myInput");
input.selectionStart = 2;
input.selectionEnd = 5;
</script>
Run Code Online (Sandbox Code Playgroud)
这是我用于 IE 6 的代码
input.select();
var sel = document.selection.createRange();
sel.collapse();
sel.moveStart('character', this.SelectionStart);
sel.collapse();
sel.moveEnd('character', this.SelectionEnd - this.SelectionStart);
sel.select();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6268 次 |
| 最近记录: |