简体   繁体   English

如何在html / javascript中创建“复制到剪贴板”按钮

[英]How to create “copy to clipboard” button in html/javascript

<html>
<input type="button" id="btnSearch" value="Search" onclick="GetValue();" />
<p id="message" ></p>

<script>
function GetValue()
{
    var myarray= new Array("item1","item2","item3");
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
}
</script>

</html>

this is the code and when i generate a random word lets say "item1" shows ,how can i add a button below it that when i click it copies the "item1" 这是代码,当我生成一个随机单词让我们说“item1”显示,我怎么能在它下面添加一个按钮,当我点击它复制“item1”

I've added some lines to your code ,try this it works ! 我在你的代码中添加了一些代码行,试试这个就行了!

 <html> <input type="button" id="btnSearch" value="Search" onclick="GetValue();" /> <p id="message" ></p><br> <button onclick="copyToClipboard('message')">Copy</button> <script> function GetValue() { var myarray= new Array("item1","item2","item3"); var random = myarray[Math.floor(Math.random() * myarray.length)]; //alert(random); document.getElementById("message").innerHTML=random; } function copyToClipboard(elementId) { var aux = document.createElement("input"); aux.setAttribute("value", document.getElementById(elementId).innerHTML); document.body.appendChild(aux); aux.select(); document.execCommand("copy"); document.body.removeChild(aux); } </script> </html> 

You're looking for the script to be: 您正在寻找以下脚本:

function GetValue()
{
    var myarray = new Array("item1", "item2", "item3");
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    var message = document.getElementById("message");
    message.value = random;
    message.select();
    document.execCommand('copy');
}

The message element needs to be a selectable element, ie a text input or textarea: <input id="message"> message元素需要是一个可选元素,即文本输入或textarea: <input id="message">

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

相关问题 如何使用 html 和 javascript 创建复制按钮? - How to create copy button using html and javascript? 是否可以使用HTML,JavaScript和ActionScript在不使用zClip的情况下创建“复制到剪贴板”按钮? - Any way to use HTML, JavaScript, and ActionScript to create a Copy to Clipboard button WITHOUT using zClip? 如何使用JavaScript复制到剪贴板并使其成为HTML? - How to Copy to Clipboard in JavaScript and make it HTML? 如何将 html 和纯文本复制到 javascript 中的剪贴板? - How to copy html and plain text to clipboard in javascript? 如何使用 HTML 或 JavaScript 将特定代码块中的文本复制到剪贴板? - How can I copy text in a specific code block to the clipboard on button click with HTML or JavaScript? 如何使用Javascript中的复制按钮将文本框的上下文复制到剪贴板 - How to copy the contexts of textbox into clipboard using the copy button in Javascript Greasemonkey \\ JavaScript复制到剪贴板按钮 - Greasemonkey\JavaScript Copy to Clipboard button 如何复制到剪贴板 javascript - How to copy to clipboard javascript 创建“自动复制到剪贴板”javascript - Create 'Automatic copy to clipboard' javascript 单击按钮后,如何将干净的HTML复制到剪贴板? - How do I copy clean HTML to clipboard on button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM