jquery 在光标处插入文字


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="common/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
(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.length;
$t.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
}
});
})(jQuery);
</script>
</head>
<body>
<textarea id="" style="width:200px;height:50px"></textarea>
<span onclick="$('textarea').insertAtCaret('$$模板$$')">$$模板$$</span>
</body>
</html>

posted @ 2021-11-23 09:13  罗华熔  阅读(96)  评论(0)    收藏  举报