The example in this article describes how jQuery implements inserting characters or expressions at specified positions in textarea. Share it with everyone for your reference. The specific implementation method is as follows:
1. Function definition
(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. Call method
$("#textareaId").insertAtCaret("New Emoticon");
I hope this article will be helpful to everyone’s jQuery programming.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn