JS在TextArea光标位置插入文字+移动光标到文字末尾
来源:互联网 发布:软件piano 编辑:程序博客网 时间:2024/05/16 06:14
在网上看了很多用js 将光标移动到textarea的最后,但是看了很多方法,不管用,浪费时间,最近用到一个新方法
=IE支持document.selection
=Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性
function insertText(obj,str) {
obj.focus();
if (document.selection) {
var sel = document.selection.createRange();
sel.text = str;
} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
var startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
function moveEnd(obj){
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character',len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
if (document.selection) {
var sel = document.selection.createRange();
sel.text = str;
} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
var startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
function moveEnd(obj){
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character',len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}
0 0
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- 【记录】JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS往textarea的光标所在处添加文字+将光标移动到文字末尾
- 如何在textarea的光标位置插入文字
- 将input中的光标移动到文字的末尾后,怎么用js显示光标当前的位置?
- js 将光标移动到textarea的最后和光标位置插入内容
- javascript控制在光标位置插入文字
- 移动光标到末尾
- 在TextArea中光标位置插入值
- 在 textarea 中光标位置插入字符串
- Ext TextArea 在光标位置插入文本
- textbox光标位置不在文字末尾的问题,位置不确定
- Extjs4中的textfiled获得光标位置,并在光标后插入文字
- JS定位光标在textarea中的位置
- JS在textarea光标处插入文本
- 将文字插入到光标处!!
- oracle字符集的查看与修改
- UIView+Border(添加任意一边)
- 父类类型的引用指向子类的对象
- think in java第五章笔记
- Hbase配置手册
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- Oracle执行计划详解
- Web资源访问及HTTP详解
- LeetCode Unique Paths II
- Transtable Tablespace 练习
- Android中的触屏事件
- 500 OOPS:cannot change directory:/home/***
- 谷歌浏览器快捷键大全
- 生产者消费者模型,循环队列实现