【记录】JS在TextArea光标位置插入文字+移动光标到文字末尾
2016-12-02 11:26
609 查看
=IE支持document.selection
=Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性
==
function insertText(obj,str) {
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;
}
}
=Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性
==
function insertText(obj,str) {
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;
}
}
相关文章推荐
- 【记录】JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字并实现移动光标到文字末尾
- JS在TextArea光标位置插入文字并实现移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- javascript实现在textarea光标位置插入文字并移动光标到文字末尾
- 将input中的光标移动到文字的末尾后,怎么用js显示光标当前的位置?
- JS往textarea的光标所在处添加文字+将光标移动到文字末尾
- js 将光标移动到textarea的最后和光标位置插入内容
- textarea在光标位置插入文字
- 用js在TextArea中的光标位置插入内容的方法(比较完美)
- 如何在textarea的光标位置插入文字
- 在光标选中位置插入文字js
- JS获取textarea的光标位置并插入内容(转)
- js实现textarea中光标位置插入你想要插入的内容
- 如何在textarea的光标位置插入文字
- js控制光标移动到文本对象的指定位置
- JS插入今明日期文本到指定光标位置
- 在TextArea中光标位置插入值