5

我在这里找到了 JQuery InsertAtCaret 函数但是没有详细说明如何使用它。我已经尝试了很多以了解如何使用它,但找不到任何方法。这是功能。

$.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();
        }
    });
};

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

4

2 回答 2

6

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

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

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

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

Working Demo

于 2013-04-12T16:24:57.320 回答
0

Firefox 最近为我破解了上面的代码,我不得不做出改变。

jQuery.fn.extend({insertAtCaret: function(myValue){
    return this.each(function(i) {
        if (document.selection) {
            //For browsers like Internet Explorer
            //log("INSERT IE: " + myValue);
            this.focus();
            var sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        } else if (this.selectionStart || this.selectionStart == '0') {
            //For browsers like Firefox and Webkit based
            //log("INSERT Firefox: " + myValue);
            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 if($(this).hasClass("ql-editor")){
            //log("INSERT QL-EDITOR: " + myValue);
            var range = window.getSelection().getRangeAt(0);
            var startPos = range.startOffset;
            var endPos = range.endOffset;
            var value = $(this).html();
            $(this).html(value.substring(0, startPos) + myValue + value.substring(endPos, value.length));
            this.focus();
        } else {
            //log("INSERT Unknown: " + myValue);
            this.value += myValue;
            this.focus();
        }
        //log(this);
    });
}});
于 2021-06-14T22:12:07.887 回答