亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery 在光標(biāo)定位的地方插入文字的插件

 更新時間:2012年05月10日 22:14:56   作者:  
jQuery 在光標(biāo)定位的地方插入文字的插件的實現(xiàn)代碼,通過獲取光標(biāo)位置,然后插入文字
核心代碼:
復(fù)制代碼 代碼如下:

(function($){
$.fn.extend({
"insert":function(value){
//默認(rèn)參數(shù)
value=$.extend({
"text":"123"
},value);
var dthis = $(this)[0]; //將jQuery對象轉(zhuǎn)換為DOM元素
//IE下
if(document.selection){
$(dthis).focus(); //輸入元素textara獲取焦點
var fus = document.selection.createRange();//獲取光標(biāo)位置
fus.text = value.text; //在光標(biāo)位置插入值
$(dthis).focus(); ///輸入元素textara獲取焦點
}
//火狐下標(biāo)準(zhǔn)
else if(dthis.selectionStart || dthis.selectionStart == '0'){
var start = dthis.selectionStart;    //獲取焦點前坐標(biāo)
var end =dthis.selectionEnd;   //獲取焦點后坐標(biāo)
    //以下這句,應(yīng)該是在焦點之前,和焦點之后的位置,中間插入我們傳入的值 .然后把這個得到的新值,賦給文本框
      dthis.value = dthis.value.substring(0, start) + value.text + dthis.value.substring(end, dthis.value.length); }
    //在輸入元素textara沒有定位光標(biāo)的情況
      else{
          this.value += value.text; this.focus();
      };
      return $(this);
    }
  })
})(jQuery)

主要思路:
  當(dāng)點擊某個元素的時候,讓一個輸入框,插入指定的值。?
  1.當(dāng)點擊某個元素的時候,應(yīng)該讓輸入框獲取焦點,因為只有獲得了焦點,才能在里面輸入值;
  IE下:document.selection.createRange()
  FF下:var start = dthis.selectionStart;    //獲取焦點前坐標(biāo)
     var end =dthis.selectionEnd;    //獲取焦點后坐標(biāo)
  2.獲取當(dāng)前輸入框焦點的位置
  3.將值插入到輸入框焦點的位置;
  4.再次獲取焦點;保證光標(biāo)在輸入框內(nèi) 
在線演示: http://demo.jb51.net/js/2012/myfocustext/
打包下載: http://chabaoo.cn/jiaoben/44153.html

相關(guān)文章

最新評論