如何使用JS复制文本并将其粘贴到textarea中?

d

我正在寻找一种解决方案,该方法如何复制文本,然后在textarea中自动粘贴新文本。我找到了解决方案,但是基于jquery,我正在寻找关于干净js的简单内容。

function copyToClipboard(elementId) {

  // Create a "hidden" input
  var aux = document.createElement("input");

  // Assign it the value of the specified element
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append it to the body
  document.body.appendChild(aux);

  // Highlight its content
  aux.select();

  // Copy the highlighted text
  document.execCommand("copy");

  // Remove it from the body
  document.body.removeChild(aux);
  
let textarea = document.getElementById("select-this");
  textarea.focus();
}
<div class="wrapper">
  <p id="p1">P1: I am paragraph 1</p>
<p id="p2">P2: I am a second paragraph</p>
<p id="p3">P3: I am a 3 paragraph</p>
<button onclick="copyToClipboard('p1')">Copy P1</button>
<button onclick="copyToClipboard('p2')">Copy P2</button>
<button onclick="copyToClipboard('p3')">Copy P3</button>
<br/><br/>
  
  <textarea id="select-this" value="I just copied this with only JavaScript"/></textarea>
</div>

我找到了一些解决方案,但是我仍然不知道如何在按下按钮后使文本自动出现在textarea中。

克里斯·李

每次运行copyToClipboard时,将复制的值附加到textarea的值

function copyToClipboard(elementId) {

  // Create a "hidden" input
  var aux = document.createElement("input");

  // Assign it the value of the specified element
  aux.setAttribute("value", document.getElementById(elementId).innerHTML);

  // Append it to the body
  document.body.appendChild(aux);

  // Highlight its content
  aux.select();

  // Copy the highlighted text
  document.execCommand("copy");

  // Remove it from the body
  document.body.removeChild(aux);

  let textarea = document.getElementById("select-this");
  textarea.focus();
  textarea.value += document.getElementById(elementId).innerHTML
}
<div class="wrapper">
  <p id="p1">P1: I am paragraph 1</p>
  <p id="p2">P2: I am a second paragraph</p>
  <p id="p3">P3: I am a 3 paragraph</p>
  <button onclick="copyToClipboard('p1')">Copy P1</button>
  <button onclick="copyToClipboard('p2')">Copy P2</button>
  <button onclick="copyToClipboard('p3')">Copy P3</button>
  <br/><br/>

  <textarea id="select-this" value="I just copied this with only JavaScript"/></textarea>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

复制数据并将其粘贴到R中

我如何从另一张纸复制行并将其粘贴到其中有表格的纸中?

如何使用Shell从文件中的一行复制值并将其粘贴到新文件中?

当我确实从可搜索的pdf文件(使用tesseract命令创建)中复制文本并将其粘贴到记事本中时,文本被更改

VBA-如何从单个单元格复制数据并将其粘贴到合并的单元格中

如何创建EmberJS应用并将其粘贴到Chrome中

如何基于该行中的单元格值复制特定行并将其粘贴到匹配的工作表中

如何在Python中复制矩阵的一部分并将其粘贴到同一矩阵的末尾?

GAS Google脚本-如何复制值并将其粘贴到另一个单元格中

如何复制文本文件的内容并将其粘贴到从某行开始的另一个文件中?

如何复制具有通用名称的文件并将其粘贴到另一个文件夹中?

用于在vim中复制文本并将其粘贴到其他窗口的脚本

如何从主机复制文本并将其粘贴到Ubuntu Server虚拟机中的nano编辑器中

使用仪表板复制特定列中的特定行并将其粘贴到其他Excel文档中

使用循环将其复制并粘贴到新行中

复制div的内部html并将其粘贴到使用javascript的其他div中

如何复制多个文本框文本并将其粘贴到其他文本框中?

如何从Internet获取图像并将其粘贴到ListView中?

使用Vlookup使用VBA复制数据并将其粘贴到单独的工作表中

复制每行并将其粘贴到VBA中

从浏览器复制所有文本并将其粘贴到txt文件中并保存。VB脚本

如何在Excel VBA中复制筛选数据范围并将其粘贴到新工作表中(不使用剪贴板)

如何从PDF复制文本并将其粘贴到其他保留文本格式的应用程序中?

如何复制附加在工作表上的图像并将其粘贴到新工作表中

复制引号内的文本并将其粘贴到同一行?

Excel VBA 从 Excel 复制范围并将其粘贴到 Word 标题文本框

复制文件中的部分文本并将其粘贴到文件内同一行的其他位置

如何使用 PHP 获取文本输入并将其粘贴到链接前?

如何有选择地从文件中复制详细信息并将其粘贴到新文件中?