將鼠標(biāo)焦點(diǎn)定位到文本框最后(代碼分享)
更新時間:2017年01月11日 11:59:30 作者:Hi.wz
本文主要分享了將鼠標(biāo)焦點(diǎn)定位到文本框最后的實(shí)例代碼。具有很好的參考價值,下面跟著小編一起來看下吧
經(jīng)測試,兼容IE8
//設(shè)置焦點(diǎn)相關(guān)---begin
//用法:$("#txtInput").val("你好").focusEnd();
$.fn.setCursorPosition = function (position) {
if (this.lengh == 0) return this;
return $(this).setSelection(position, position);
}
$.fn.setSelection = function (selectionStart, selectionEnd) {
if (this.lengh == 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 () {
if (this.val() != undefined) {
this.setCursorPosition(this.val().length);
}
}
//設(shè)置焦點(diǎn)相關(guān)---end
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Jquery 復(fù)選框取值兼容FF和IE8(測試有效)
Jquery 復(fù)選框取值的文章網(wǎng)上有很多的,不過可以同時兼容FF和IE8的確實(shí)沒有幾個,下面有個不錯的方法經(jīng)測試有效2013-10-10
input 和 textarea 輸入框最大文字限制的jquery插件
input 和 textarea 輸入框最大文字限制的jquery插件,需要的朋友可以參考下。2011-10-10
使用jquery+CSS3實(shí)現(xiàn)仿windows10開始菜單的下拉導(dǎo)航菜單特效
本文是基于jquery和css3實(shí)現(xiàn)的仿windows10開始菜單的下拉導(dǎo)航菜單特效,代碼超簡單,感興趣的朋友一起看看吧2015-09-09

