文本复制并粘贴到一个文本使用jquery
答
如果你有一个<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());
});
答
因此,假设您的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
您目前使用的代码是?你有什么尝试? – chembrad