简体   繁体   English

如何将按钮的内容复制到剪贴板(JS)

[英]How to copy contents of a button to clipboard (JS)

I want to be able to copy the text of a button to the clipboard. 我希望能够将按钮的文本复制到剪贴板。 I am able to retrieve the innerText of the button and log it to console but I am not able to add it to the selection and then ultimately add it to the clipboard with 'document.execCommand("copy");'. 我能够检索按钮的innerText并将其记录到控制台,但我无法将其添加到选择中,然后最终使用'document.execCommand(“copy”);'将其添加到剪贴板。 Any ideas? 有任何想法吗?

 $(document).ready(function() { $('button').on('click', function() { var copyText = this.innerText; console.log(copyText); copyText.select; document.execCommand("copy"); /* Alert the copied text */ alert("Copied the text: " + copyText); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <div> <ul id="test"> <li> <button class="copy-button" id="button1">Test1</button> </li> <li> <button class="copy-button" id="button2">Test2</button> </li> <li> <button class="copy-button" id="button3">Test3</button> </li> </ul> </div> 

Ok, insted of adding just a string to a clipboard, you must make this: 好的,只是在剪贴板中添加一个字符串,你必须这样做:

Create a textarea, add your stiring to the textarea, and copy it from there, and then delete the textarea. 创建一个textarea,将您的stiring添加到textarea,然后从那里复制它,然后删除textarea。

Hope this helps: 希望这可以帮助:

 function copyStringToClipboard () { var str = document.getElementById("btn1").innerText; // Create new element var el = document.createElement('textarea'); // Set value (string to be copied) el.value = str; // Set non-editable to avoid focus and move outside of view el.setAttribute('readonly', ''); el.style = {position: 'absolute', left: '-9999px'}; document.body.appendChild(el); // Select text inside element el.select(); // Copy text to clipboard document.execCommand('copy'); // Remove temporary element document.body.removeChild(el); } 
 <button onclick="copyStringToClipboard()" id = 'btn1'>Click me</button> <input type="text" placeholder="Paste here"> 

An execCommand method to run commands that manipulate the current editable region, such as form inputs or contentEditable elements. 一种execCommand方法,用于运行操作当前可编辑区域的命令,例如form inputscontentEditable元素。 Find the explanation from here execCommand 从这里找到execCommand的解释

You can check following answer posted by @Hani for this type of question, I've used Hani solution here to solve your issue. 您可以查看@Hani针对此类问题发布的以下答案 ,我在这里使用Hani解决方案来解决您的问题。

 $(document).ready(function() { $('button').on('click', function() { var copyText = this.innerText; // console.log(copyText); // copyText.select; // document.execCommand("copy"); var textarea = document.createElement('textarea'); textarea.id = 'temp_element'; textarea.style.height = 0; document.body.appendChild(textarea); textarea.value = copyText; var selector = document.querySelector('#temp_element') selector.select(); document.execCommand('copy'); document.body.removeChild(textarea); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <div> <ul id="test"> <li> <button class="copy-button" id="button1">Test1</button> </li> <li> <button class="copy-button" id="button2">Test2</button> </li> <li> <button class="copy-button" id="button3">Test3</button> </li> </ul> </div> 

Basically, since you can't use button.select(), you want to create an element that you can select in order to copy to clipboard. 基本上,由于您无法使用button.select(),因此您需要创建一个可以选择的元素以便复制到剪贴板。 So by creating a temporary input element with the same content as your button, you bypass that. 因此,通过创建一个与按钮具有相同内容的临时输入元素,可以绕过它。 You can now select the input element and simply copy it. 您现在可以选择输入元素并简单地复制它。 You can test this by using "paste" directly after running the code. 您可以在运行代码后直接使用“粘贴”进行测试。

In your HTML: 在您的HTML中:

<button id='btnID'>Success</button>'

In your JS: 在你的JS中:

var input = document.createElement('input');
input.value = document.getElementById('btnID').innerHTML;
input.id = 'inputID';
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);

I think this is the best solution for you, Try it 我认为这是最适合您的解决方案,试一试

 function CopyText(btnText,btn) { input=$(btn).prev(); $(input).val(btnText); var copyText = $(input); copyText.select(); document.execCommand("copy"); alert("Copied the text: " +$(copyText).val()); } 
 .btns{ position: absolute; width:0.1px; height:0.1px; z-index:-99999999999; } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <ul class="test"> <li> <input type="text" class="btns" value=""> <button onclick="CopyText(this.innerText,this)">HELLO222</button> </li> <li> <input type="text" class="btns" value=""> <button onclick="CopyText(this.innerText,this)">HELLO7777</button> </li> </ul> </div> 

the idea is to put the text of the button as a value of text box, and this textbox must be hidden, the problem is copy function does not work with "hidden fields" so the solution for this issue is to set the textbox to very small dimensions I set it 1.0px for each width1 and height and z-index` with long negative value to make it Semi-hidden. 这个想法是把按钮的文本作为文本框的值,这个文本框必须隐藏,问题是复制功能不能用“隐藏字段”,所以这个问题的解决方案是将文本框设置为非常小尺寸我将它1.0px每个width1 and高度and Z-index`长负值,使其半隐藏。 good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM