简体   繁体   English

将选定的文本粘贴到另一个文本区域

[英]Paste selected text to another textarea

is it possible with pure JS to paste selected text into specified textarea?纯JS可以将选定的文本粘贴到指定的文本区域吗? In my case, I want to select text in one of the textareas and when ctrl & A is pressed, selected text would paste into last (V1) textarea.就我而言,我想在其中一个文本区域中输入 select 文本,当按下 ctrl & A 时,选定的文本将粘贴到最后一个(V1)文本区域。

I have found similar case ( https://jsfiddle.net/QenBV/1/ ), but it's only made for 1 input textarea, but I have large number of textareas.我发现了类似的情况( https://jsfiddle.net/QenBV/1/ ),但它仅适用于 1 个输入文本区域,但我有大量的文本区域。

 function Addkey(e) { if (e.ctrlKey && e.keyCode == 65) { e.preventDefault(); document.execCommand("copy"); } } document.addEventListener('keydown', Addkey, false);
 <textarea>Text1</textarea><br/> <textarea>Text2</textarea><br/> <textarea>Text3</textarea><br/> <p></p> <hr/> <p></p> <textarea id="V1"></textarea><br/>

As per your question, the following one is your first text area from where you want to copy text to others根据您的问题,以下是您要将文本复制到其他人的第一个文本区域

<textarea rows="5" cols="80" id="input" onkeyup="copySelected(event)" >One two three</textarea>

And you want to paste the selected text to the following textarea's并且您想将选定的文本粘贴到以下文本区域的

<textarea rows="5" cols="80" id="selection" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection1" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection2" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection3" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection4" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection5" class="selection"></textarea>
<textarea rows="5" cols="80" id="selection6" class="selection"></textarea>

This would be your pure javascript code.这将是您的纯 javascript 代码。

<script>
function getSelectedText(el) {
    if (typeof el.selectionStart == "number") {
        return el.value.slice(el.selectionStart, el.selectionEnd);
    } else if (typeof document.selection != "undefined") {
        var range = document.selection.createRange();
        if (range.parentElement() == el) {
            return range.text;
        }
    }
    return "";
}

function copySelected(event) {

    if(event.ctrlKey && ())
    var srcTextarea = document.getElementById("input");
    var destTextarea = document.getElementsByClassName("selection");
    var txtd=getSelectedText(srcTextarea);
    <!-- if u want to paste the text only last one of your textarea -->
    
    destTextarea[destTextarea.length-1].value = txtd;
    
    <!-- if u want to paste the text all of the textarea then use following code block -->
    /*
     for(i=0; i<destTextarea.length; i++){
      destTextarea[i].value = txtd;
     }
    */

}

</script>

Here, when you press "ctrl + a" in your first textarea, then the text will be paste into all other textarea.在这里,当您在第一个文本区域中按“ctrl + a”时,文本将被粘贴到所有其他文本区域中。

You can simply find out the focused text field using javascript/jquery and then copy the text when selected and concatenate it to the existing text in V1 text area Or you can simply add a button at the end of the text fields and when the button is clicked you can find texts of every text field using javascript/jquery and concatenate each text and paste into v1 text area您可以简单地使用 javascript/jquery 找出焦点文本字段,然后在选择时复制文本并将其连接到 V1 文本区域中的现有文本或者您可以简单地在文本字段的末尾添加一个按钮,当按钮是单击您可以使用 javascript/jquery 查找每个文本字段的文本并将每个文本连接起来并粘贴到 v1 文本区域

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

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