基于jQuery實(shí)現(xiàn)的設(shè)置文本區(qū)域的光標(biāo)位置
如何使用jQuery在文本框中設(shè)置光標(biāo)位置?我有一個帶有內(nèi)容的文本字段,并且我希望光標(biāo)在焦點(diǎn)位于特定的偏移位置,該如何實(shí)現(xiàn)呢?
實(shí)現(xiàn)方法一:
這是一個jQuery解決方案:
$.fn.selectRange = function(start, end) {
if(end === undefined) {
end = start;
}
return this.each(function() {
if('selectionStart' in this) {
this.selectionStart = start;
this.selectionEnd = end;
} else if(this.setSelectionRange) {
this.setSelectionRange(start, end);
} else if(this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
有了這個,你可以做
$('#elem').selectRange(3,5); // select a range of text
$('#elem').selectRange(3); // set cursor position
實(shí)現(xiàn)方法二:
$.fn.setCursorPosition = function(position){
if(this.length == 0) return this;
return $(this).setSelection(position, position);
}
$.fn.setSelection = function(selectionStart, selectionEnd) {
if(this.length == 0) return this;
input = this[0];
if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
} else if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
return this;
}
$.fn.focusEnd = function(){
this.setCursorPosition(this.val().length);
return this;
}
現(xiàn)在,您可以通過調(diào)用以下任何元素將焦點(diǎn)移至任何元素的結(jié)尾
$(element).focusEnd();
方法三
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}
調(diào)用辦法:
setCaretToPos(document.getElementById("YOURINPUT"), 4);
jquery中文本域光標(biāo)操作(選中、添加、刪除、獲?。?/strong>
1、獲取光標(biāo)位置:$(elem).iGetFieldPos();
2、設(shè)置光標(biāo)位置:$(elem).iSelectField(start);
3、選中指定位置內(nèi)的字符:$(elem).iSelectField(start,end);
4、選中指定的字符:$(elem).iSelectStr(str);
5、在光標(biāo)之后插入字符串:$(elem).iAdd(str);
6、刪除光標(biāo)前面(-n)或者后面(n)的n個字符:$(elem).iDel(n);
這篇文章就介紹到這了,希望大家以后多多支持腳本之家。
相關(guān)文章
jQuery利用FormData上傳文件實(shí)現(xiàn)批量上傳
這篇文章主要為大家詳細(xì)介紹了jQuery利用FormData上傳文件實(shí)現(xiàn)批量上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
解決jquery的ajax調(diào)取后端數(shù)據(jù)成功卻渲染失敗的問題
今天小編就為大家分享一篇解決jquery的ajax調(diào)取后端數(shù)據(jù)成功卻渲染失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
用原生JavaScript實(shí)現(xiàn)jQuery的$.getJSON的解決方法
本篇文章介紹了,用原生JavaScript實(shí)現(xiàn)jQuery的$.getJSON的解決方法。需要的朋友參考下2013-05-05
jQuery插件HighCharts實(shí)現(xiàn)的2D堆條狀圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts實(shí)現(xiàn)的2D堆條狀圖效果,結(jié)合完整實(shí)例形式分析了jQuery插件HighCharts繪制條狀圖的具體實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
Json實(shí)現(xiàn)異步請求提交評論無需跳轉(zhuǎn)其他頁面
Json實(shí)現(xiàn)異步請求,效果即為,在輸入框中輸入相關(guān)文字,點(diǎn)擊提交,下方會自動將書寫的文字進(jìn)行展示,無需跳轉(zhuǎn)其他頁面2014-10-10

