简体   繁体   English

将所选文本从一个文本区域复制到另一个文本区域

[英]Copy selected text from one textarea to another

I have two <textarea> s. 我有两个<textarea> One with id="input" and the other with id="selection" . 一个id="input" ,另一个id="selection"

<textarea id="input"> will contain some HTML. <textarea id="input">将包含一些HTML。 The user will select some text in this textarea, click a button and the selected text will be copied to <textarea id="selection"> . 用户将在此textarea中选择一些文本,单击一个按钮,所选文本将被复制到<textarea id="selection">

I can use jQuery or just vanilla JavaScript and I'd like it to work in IE7+, Safari and Firefox. 我可以使用jQuery或只是vanilla JavaScript,我希望它可以在IE7 +,Safari和Firefox中使用。

There's only one way I've been able to do it. 我只有一种方法可以做到这一点。 The problem you're running into, as you may be aware, is that when you click the button (thus firing the event to copy the selection), the textarea loses focus, and thereby there's no text selected. 正如您可能知道的那样,您遇到的问题是,当您单击按钮(从而触发事件以复制选择)时,textarea会失去焦点,因此没有选择任何文本。

So as a workaround, I styled a div to look (kinda) like a textarea. 因此,作为一种解决方法,我设计了一个div,看起来像有点像textarea。 That seems to work: 这似乎有效:

<style type="text/css">
    .textarea { 
        border:1px solid black; 
        width:200px; 
        height:100px; 
        overflow-y: auto; 
        float:left; 
    }
</style>

The markup then looks like this: 然后标记看起来像这样:

<div id="input" class="textarea">This is a test</div>
<textarea id="selection"></textarea>

<button id="theButton">Copy</button>

And finally, the script: 最后,剧本:

var selText = "";

$( document ).ready( function() {
    $( '#theButton' ).mousedown( function() {
        $( '#selection' ).val( getSelectedText() );
    });
});

function getSelectedText(){
    if ( window.getSelection ) {
        return window.getSelection().toString();
    }
    else if ( document.getSelection ) {
        return document.getSelection();
    } else if ( document.selection ) {
        return document.selection.createRange().text;
    }
} 

To give full credit where it's due, I got the getSelectedText() method from http://esbueno.noahstokes.com/post/92274686/highlight-selected-text-with-jquery 为了给予应有的充分信用,我从http://esbueno.noahstokes.com/post/92274686/highlight-selected-text-with-jquery获得了getSelectedText()方法。

The following will do it: 以下将这样做:

See it in action: http://www.jsfiddle.net/QenBV/1/ 看到它在行动: http//www.jsfiddle.net/QenBV/1/

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() {
    var srcTextarea = document.getElementById("input");
    var destTextarea = document.getElementById("selection");
    destTextarea.value = getSelectedText(srcTextarea);
}

<input type="button" onclick="copySelected()" value="copy selected">

with jquery you can do as below 使用jquery,您可以执行以下操作

 $('#selection').val($('#input').val());

http://api.jquery.com/val/ http://api.jquery.com/val/

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

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