Home > Web Front-end > JS Tutorial > JQuery implementation code for inserting content at the cursor position_jquery

JQuery implementation code for inserting content at the cursor position_jquery

WBOY
Release: 2016-05-16 18:25:02
Original
1059 people have browsed it
复制代码 代码如下:

(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);

使用方法:
复制代码 代码如下:
$(selector).insertAtCaret("value");
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template