[英]How to copy text and paste into a textarea using JS?
I am looking for a solution how to copy text and then paste a new text automatic in textarea. 我正在寻找一种解决方案,该方法如何复制文本,然后在textarea中自动粘贴新文本。 I found solutions, but based on jquery I'm looking for something simple on clean js.
我找到了解决方案,但是基于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>
I found some solutions, but I still do not know how to make the text automatically appear in textarea after pressing the button. 我找到了一些解决方案,但是我仍然不知道如何在按下按钮后使文本自动出现在textarea中。
ummm...You are REALLY over-complicating stuff... 嗯...你真的太复杂了...
Just use the following JS: 只需使用以下JS:
let textarea = document.getElementById("select-this"); textarea.focus(); function changeTextarea(elementId) { textarea.innerHTML = document.body.querySelector(elementId).innerHTML; }
and edit the HTML of the buttons as follows: 并按如下所示编辑按钮的HTML:
<button onclick="changeTextarea('#p1')">Copy P1</button> <button onclick="changeTextarea('#p2')">Copy P2</button> <button onclick="changeTextarea('#p3')">Copy P3</button>
You don't need to copy and then paste the values of the paragraphs to the <textarea>
. 您无需复制然后将段落的值粘贴到
<textarea>
。 Just change it using the innerHTML
property... 只需使用
innerHTML
属性对其进行更改...
append the copied value to value of textarea everytime you run copyToClipboard 每次运行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>
I've a simple solution for that, just using the part of the code you have. 为此,我有一个简单的解决方案,只需使用部分代码即可。
function copyToClipboard(elementId) { var text = document.getElementById(elementId).innerHTML; let textarea = document.getElementById("select-this"); textarea.innerHTML = text; textarea.focus(); }
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.