2015-11-01 220 views

回答

1

如果你有一個<div>元素,像這樣的原文:

<div id="original"> 
    Original text... 
</div> 

和按鈕像這樣:

<button id="copy">Copy Text</button> 

<textarea>像這樣:

<textarea id="paste"></textarea> 

您可以簡單地使用jQuery來獲取原始的值並將其粘貼到<textarea>像這樣:

$("#copy").click(function() { 
    $("#paste").val($("original").text()); 
}); 

this example

1

因此,假設您的ID爲original的div中的「原始文本」,複製按鈕的ID爲copy,文本區域的ID爲paste-here。然後,這個簡單的代碼段應該給它:

//When the user clicks the copy button... 
$('#copy').click(function() { 
    //Take the text of the div... 
    var text = $('#original').text(); 
    //...and put it in the div: 
    $('#paste-here').val(text); 
}); 

這將替換文本區的原文內容。如果您只是想將其添加到最後,請改爲執行此操作:

//Take the text of the textarea, a linebreak, and the text of the div... 
    var text = $('#paste-here').val() + '\n' + $('#original').text(); 
    //...and put it in the div: 
    $('#paste-here').val(text); 
+0

在一個關於話題的筆記中,我以爲你正在用'tarea'講西班牙語,並且想知道這跟這個有什麼關係,然後才意識到它的意思是「textarea」。哇,我覺得很愚蠢。 –

+1

哈哈我知道沒有西班牙語。命名元素就像元素類型的簡短形式是可怕的命名一樣,所以我將它更新爲更具啓發性的內容。 :-) – Anders

相關問題