Javascript 使用jquery复制文本并粘贴到文本区域
在论坛中,我想复制原始帖子并引用它,然后使用jQuery将其粘贴到文本区域:Javascript 使用jquery复制文本并粘贴到文本区域,javascript,jquery,textarea,Javascript,Jquery,Textarea,在论坛中,我想复制原始帖子并引用它,然后使用jQuery将其粘贴到文本区域: 我找到了一封信。但是,它并不适用于我真正需要的内容。如果在元素中有原始文本,如下所示: <div id="original"> Original text... </div> 请参阅。因此,假设“原始文本”位于一个div中,ID为original,复制按钮的ID为copy,文本区域的ID为粘贴在此处。然后,这个简单的代码片段应该被添加到它: //When the user click
我找到了一封信。但是,它并不适用于我真正需要的内容。如果在
元素中有原始文本,如下所示:
<div id="original">
Original text...
</div>
请参阅。因此,假设“原始文本”位于一个div中,ID为original
,复制按钮的ID为copy
,文本区域的ID为粘贴在此处
。然后,这个简单的代码片段应该被添加到它:
//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);
在一个离题的便条上,我以为你在和tarea讲西班牙语,在意识到它的意思是“textarea”之前,我想知道这与此有什么关系。哇,我觉得自己很笨。哈哈,我不懂西班牙语。不过,按照元素类型的缩写形式命名元素是一种可怕的命名方式,因此我将其更新为更具指导性的形式。:-)到目前为止你有什么代码?你尝试了什么?
$("#copy").click(function() {
$("#paste").val($("original").text());
});
//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);