基于jQuery的輸入框在光標位置插入內(nèi)容, 并選中
更新時間:2011年10月29日 01:22:13 作者:
基于jQuery的輸入框在光標位置插入內(nèi)容, 并選中功能的實現(xiàn)代碼,需要的朋友可以參考下。
復制代碼 代碼如下:
//
//使用方法
//$(文本域選擇器).insertContent("插入的內(nèi)容");
//$(文本域選擇器).insertContent("插入的內(nèi)容",數(shù)值); //根據(jù)數(shù)值選中插入文本內(nèi)容兩邊的邊界, 數(shù)值: 0是表示插入文字全部選擇,-1表示插入文字兩邊各少選中一個字符。
//
//在光標位置插入內(nèi)容, 并選中
(function($) {
$.fn.extend({
insertContent: function(myValue, t) {
var $t = $(this)[0];
if (document.selection) { //ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
sel.select();
}
} else if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t, $t.selectionEnd + t);
this.focus();
}
}
else {
this.value += myValue;
this.focus();
}
}
})
})(jQuery);
您可能感興趣的文章:
相關(guān)文章
完美兼容各大瀏覽器的jQuery插件實現(xiàn)圖片切換特效
網(wǎng)友zoeDylan寫的原創(chuàng)jquery插件,實現(xiàn)了圖片輪播功能,jquery插件名稱為zoeDylan.ImgChange,圖片路徑、跳轉(zhuǎn)鏈接、提示標題都是有動態(tài)數(shù)組來配置,jquery插件可靈活配置6個參數(shù), height高、width寬、mgs圖片地址、links點擊地址、tips圖片說明、timers自動切換時間,2014-12-12jQuery根據(jù)ID獲取input、checkbox、radio、select的示例
獲取input、checkbox、radio、select的方法有很多,下面本例為大家介紹下根據(jù)ID來獲取2014-08-08EasyUI中datagrid在ie下reload失敗解決方案
這篇文章主要介紹了EasyUI中datagrid在ie下reload失敗解決方案,結(jié)合網(wǎng)上搜集來的幾種方案,最終解決了這個問題,分享給大家,希望對大家能夠有所幫助。2015-03-03jQuery實現(xiàn)仿新浪微博浮動的消息提示框(可智能定位)
這篇文章主要介紹了jQuery實現(xiàn)仿新浪微博浮動的消息提示框,具有智能定位功能,涉及jQuery控制頁面元素浮動效果的實現(xiàn)技巧,需要的朋友可以參考下2015-10-10jquery $.ajax()取xml數(shù)據(jù)的小問題解決方法
今天想做一個用$.ajax()從xml中讀取數(shù)據(jù)的這么一個異步交互過程2010-11-11jquer之a(chǎn)jaxQueue簡單實現(xiàn)代碼
手頭不是很忙,突然想到了ajax queue這個概念,之前貌似有看技術(shù)文章中提到過這個,就想著用jquery來實現(xiàn)一下,思想比較簡單2011-09-09