我的问题不是重复的,我没有使用Jquery,我没有使用textarea。我想使用普通javascript将变量的值复制到剪贴板中。
我想使用按钮旁边的onClick Eventlistener将存储在变量中的文本字符串复制到剪贴板中。
我尝试修改此example,它使用一个输入字段,但是不起作用。我才刚刚开始了解Javascript,所以请客气。另外,我也在寻找一种不使用库的解决方案。
SELECT GROUP_CONCAT(DISTINCT action_id ORDER BY action_id)
FROM tbl_action;
function myFunction() {
var copyText = "This is a Test"
copyText.select();
document.execCommand("copy");
}
答案 0 :(得分:1)
您可以尝试这样的事情。
function myFunction() {
// variable content to be copied
var copyText = "Test"
// create an input element
let input = document.createElement('input');
// setting it's type to be text
input.setAttribute('type', 'text');
// setting the input value to equal to the text we are copying
input.value = copyText;
// appending it to the document
document.body.appendChild(input);
// calling the select, to select the text displayed
// if it's not in the document we won't be able to
input.select();
// calling the copy command
document.execCommand("copy");
// removing the input from the document
document.body.removeChild(input)
}
<button onclick="myFunction()">Copy text</button>
答案 1 :(得分:0)
HTMLInputElement.select()方法选择一个元素或具有文本字段的元素中的所有文本。