JS在TextArea光標(biāo)位置插入文字并實現(xiàn)移動光標(biāo)到文字末尾
更新時間:2013年06月21日 16:28:12 作者:
JS在TextArea光標(biāo)位置插入文字+移動光標(biāo)到文字末尾,F(xiàn)irefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd屬性,具體實現(xiàn)如下,感興趣的朋友可以參考下哈
=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;
}
}
<input type="button" onclick="insertText(document.getElementById('text'),' 新文字—YoyiorLee ')" value="插入文字"></input>
<input type="button" onclick="moveEnd(document.getElementById('text'))" value="移到末尾"></input>
=Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd屬性
復(fù)制代碼 代碼如下:
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;
}
}
復(fù)制代碼 代碼如下:
<input type="button" onclick="insertText(document.getElementById('text'),' 新文字—YoyiorLee ')" value="插入文字"></input>
復(fù)制代碼 代碼如下:
<input type="button" onclick="moveEnd(document.getElementById('text'))" value="移到末尾"></input>
您可能感興趣的文章:
- JS在可編輯的div中的光標(biāo)位置插入內(nèi)容的方法
- js獲取光標(biāo)位置和設(shè)置文本框光標(biāo)位置示例代碼
- 用javascript獲取textarea中的光標(biāo)位置
- 用javascript獲取當(dāng)頁面上鼠標(biāo)光標(biāo)位置和觸發(fā)事件的對象的代碼
- JavaScript 獲取/設(shè)置光標(biāo)位置,兼容Input&&TextArea
- javascript獲得光標(biāo)所在的文本框(text/textarea)中的位置
- textbox 在光標(biāo)位置插入字符功能的js實現(xiàn)(兼容ie,firefox)
- javascript控制在光標(biāo)位置插入文字適合表情的插入
- Javascript實現(xiàn)獲取及設(shè)置光標(biāo)位置的方法
- 往光標(biāo)所在位置插入值的js代碼
- js實現(xiàn)的光標(biāo)位置工具函數(shù)示例
相關(guān)文章
Javascript Request獲取請求參數(shù)如何實現(xiàn)
使用Javascript Request獲取參數(shù)的時候總是提示出錯,本文為此問題提供詳細的解決方案,需要了解的朋友可以參考下2012-11-11
JavaScript使用百度ECharts插件繪制餅圖操作示例
這篇文章主要介紹了JavaScript使用百度ECharts插件繪制餅圖操作,結(jié)合實例形式分析了JavaScript使用百度ECharts插件繪制餅圖的原理、步驟及相關(guān)操作注意事項,需要的朋友可以參考下2019-11-11
網(wǎng)頁爬蟲之cookie自動獲取及過期自動更新的實現(xiàn)方法
這篇文章主要介紹了網(wǎng)頁爬蟲之cookie自動獲取及過期自動更新的實現(xiàn)方法,需要的朋友可以參考下2018-03-03
JS解決Date對象在IOS中的“大坑” 以及時間格式兼容問題
這篇文章主要介紹了JS解決Date對象在IOS中的“大坑” 以及時間格式兼容問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
window.showModalDialog參數(shù)傳遞中含有特殊字符的處理方法
程序運行出錯經(jīng),過檢查發(fā)現(xiàn)傳遞的數(shù)據(jù)中出現(xiàn)了#等特殊字符,瀏覽器只取到#號前面的數(shù)據(jù),后面的被截斷,下面為大家介紹下正確的處理方法2013-06-06

