JQuery在光标位置插入内容的实现代码

时间:2010-06-18    点击:112   
复制代码 代码如下:

(function($){
$.fn.extend({
insertAtCaret: function(myValue){
var $t=$(this)[0];
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
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;
}
else {
this.value += myValue;
this.focus();
}
}
})
})(jQuery);

使用方法:
复制代码 代码如下:
$(selector).insertAtCaret("value");
基于jquery的滚动新闻列表
基于Jquery的温度计动画效果
JQuery Dialog的内存泄露问题解决方法
通过jQuery打造支持汉字,拼音,英文快速定位查询的超级select插件
jquery ready()的几种实现方法小结
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved