簡體   English   中英

將選定的文本粘貼到另一個文本區域

[英]Paste selected text to another textarea

純JS可以將選定的文本粘貼到指定的文本區域嗎? 就我而言,我想在其中一個文本區域中輸入 select 文本,當按下 ctrl & A 時,選定的文本將粘貼到最后一個(V1)文本區域。

我發現了類似的情況( 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/>

根據您的問題,以下是您要將文本復制到其他人的第一個文本區域

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

並且您想將選定的文本粘貼到以下文本區域的

<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>

這將是您的純 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>

在這里,當您在第一個文本區域中按“ctrl + a”時,文本將被粘貼到所有其他文本區域中。

您可以簡單地使用 javascript/jquery 找出焦點文本字段,然后在選擇時復制文本並將其連接到 V1 文本區域中的現有文本或者您可以簡單地在文本字段的末尾添加一個按鈕,當按鈕是單擊您可以使用 javascript/jquery 查找每個文本字段的文本並將每個文本連接起來並粘貼到 v1 文本區域

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM