/**
* @param $
* 自定义jQuery扩展方法, 在光标处插入内容
*/
(function ($) {
"use strict";
$.fn.extend({
insertAtCaret : function (myValue) {
var $t = $(this)[0];
if (document.selection) {
this.focus();
var 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
textarea文本内容光标后添加文本
最新推荐文章于 2023-12-13 17:18:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)