jQuery实现在textarea指定位置插入字符或表情的方法
作者:bea
本文实例讲述了jQuery实现在textarea指定位置插入字符或表情的方法。分享给大家供大家参考。具体实现方法如下: 1. 函数定义 代码如下: (function($){ $.fn.extend({ insertAtCaret: function(myValue){ var $t=$(this)[0]; if (document.selection) { this.focus(); sel = document.selection.creat
本文实例讲述了jQuery实现在textarea指定位置插入字符或表情的方法。分享给大家供大家参考。具体实现方法如下:
1. 函数定义
代码如下:
(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);
2. 调用方法
代码如下:
$("#textareaId").insertAtCaret("新表情");
希望本文所述对大家的jQuery程序设计有所帮助。
有用 | 无用
1. 函数定义
代码如下:
(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);
2. 调用方法
代码如下:
$("#textareaId").insertAtCaret("新表情");
希望本文所述对大家的jQuery程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- javascript实时显示北京时间的方法
- jquery实现焦点图片随机切换效果的方法
- JavaScript数据结构与算法之栈详解
- jQuery实现简单的日期输入格式化控件
- Javascript数据结构与算法之列表详解
- javascript实现 百度翻译 可折叠的分享按钮列表
- 基于jquery实现的自动补全功能
- jquery实现页面百叶窗走马灯式翻滚显示效果的方法
- window.open()实现post传递参数
- js运动动画的八个知识点
- js实现最短的XML格式化工具实例
- 微信中一些常用的js方法汇总
- javascript实现checkBox的全选,反选与赋值
- jQuery通过扩展实现抖动效果的方法
- jQuery实现字符串按指定长度加入特定内容的方法
- jquery利用命名空间移除绑定事件的方法
- jQuery实现带动画效果的二级下拉导航方法
- jquery图形密码实现方法
- jQuery模拟新浪微博首页滚动效果的方法