如何使用JQuery InsertAtCaret函数

我在这里找到了JQuery InsertAtCaret函数但是没有给出如何使用它的细节。 我已经尝试了很多,以了解如何使用它,但找不到任何方法。 这是function。

$.fn.insertAtCaret = function(myValue) { return this.each(function() { var me = this; if (document.selection) { // IE me.focus(); sel = document.selection.createRange(); sel.text = myValue; me.focus(); } else if (me.selectionStart || me.selectionStart == '0') { // Real browsers var startPos = me.selectionStart, endPos = me.selectionEnd, scrollTop = me.scrollTop; me.value = me.value.substring(0, startPos) + myValue + me.value.substring(endPos, me.value.length); me.focus(); me.selectionStart = startPos + myValue.length; me.selectionEnd = startPos + myValue.length; me.scrollTop = scrollTop; } else { me.value += myValue; me.focus(); } }); }; 

我有一个文本框输入字段和下面的textarea。 我应该在哪里调用此函数,我应该给它什么值。 而且我必须提供我的textarea的参考。

这是上面插件的修改版本:

 jQuery.fn.extend({ insertAtCaret: function(myValue){ return this.each(function(i) { if (document.selection) { //For browsers like Internet Explorer this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); } else if (this.selectionStart || this.selectionStart == '0') { //For browsers like Firefox and Webkit based var startPos = this.selectionStart; var endPos = this.selectionEnd; var scrollTop = this.scrollTop; this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length); this.focus(); this.selectionStart = startPos + myValue.length; this.selectionEnd = startPos + myValue.length; this.scrollTop = scrollTop; } else { this.value += myValue; this.focus(); } }) } }); 

基本上,这个插件允许您在多个textboxtextarea插入符号处插入一段textarea 。 你可以像这样使用它:

  $('#element1, #element2, #element3, .class-of-elements').insertAtCaret('text'); 

工作演示