简体   繁体   English

将文本插入选定的文本区域

[英]insert text into selected textarea

When I click a button I want to insert some text into the currently selected textarea at the current caret position. 当我单击一个按钮时,我想在当前插入符号位置的当前选定文本区域中插入一些文本。 How do I do this with jQuery or just Javascript? 我该如何使用jQuery或仅使用Javascript?

I've come a across code that inserts text into a specific text area but for my project there are multiple textareas. 我遇到了一个跨代码,可将文本插入特定的文本区域,但是对于我的项目,有多个文本区域。

This does all what you want (except old browser support). 这可以满足您的所有需求(旧版浏览器支持除外)。

var myText = "sup?",
    lastActiveElement = null;

window.addEventListener("focusin", function(e) {
    lastActiveElement = e.target;
});

document.getElementById("go").addEventListener("click", function(e) {
    if(!lastActiveElement || lastActiveElement.tagName.toUpperCase() !== "TEXTAREA") {
        alert("no textarea selected");
        return;
    }
    var selectionStart = lastActiveElement.selectionStart,
        value = lastActiveElement.value;
    lastActiveElement.value = value.substr(0, selectionStart) + myText + value.substr(selectionStart);
});

Demo: http://jsfiddle.net/rfYZq/2/ 演示: http//jsfiddle.net/rfYZq/2/

The focusin event stores the last focused element into a var. focusin事件将最后关注的元素存储到var中。

(function ($, undefined) {
    $.fn.getCursorPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if('selectionStart' in el) {
            pos = el.selectionStart;
        } else if('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
})(jQuery);

Basically, to use it on a text box, do the following: 基本上,要在文本框中使用它,请执行以下操作:

$("#myTextBoxSelector").getCursorPosition();

I got this script from somewhere else but I can't find the bookmark for it. 我从其他地方获得了此脚本,但是找不到它的书签。

This adds a method to jQuery that will allow you to do the following: 这为jQuery添加了一个方法,使您可以执行以下操作:

$("#selectedTextarea").insertAtCaret(myText);

Edit: a working fiddle here 编辑: 这里工作的小提琴

And the code: 和代码:

jQuery.fn.extend({
insertAtCaret: function(myValue)
{
    return this.each(function()
    {
        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();
        }
    })
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM