在textarea指定位置插入字符或表情
2013-11-08 10:25
239 查看
(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);
$.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);
[代码]调用方法
1 | $( "#textareaId" ).insertAtCaret( "新表情" ); |
相关文章推荐
- 在textarea指定位置插入字符或表情
- 在textarea中鼠标指定的位置插入字符或表情
- jQuery实现在textarea指定位置插入字符或表情的方法
- jQuery实现在textarea指定位置插入字符或表情的方法
- 点击鼠标在textarea中光标当前位置插入指定字符
- js在textarea光标处插入表情字符(ie,火狐兼容)
- 光标指定位置添加字符,适用于普通textbox ,textarea,不适用于文本编辑器插件
- android开发 Java在指定的位置插入字符
- sed命令在指定位置插入字符
- pyghon list指定位置插入字符的方法
- Python:字符串中指定位置插入一个字符
- php 在字符串指定位置插入新字符的简单实现
- Python 实现字符串中指定位置插入一个字符
- 向字符串中指定位置插入字符
- 详解C语言在字符串的指定位置插入字符
- java格式化字符串,在指定位置插入指定字符串,兼容中英文以及特殊字符,例如:换行,用于解决生成pdf换行问题等问题
- PHP:将一个字符创插入到另一个字符串的指定位置
- C实例----在字符串指定位置插入字符
- 在指定位置插入字符
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符