我想知道是否有办法将文本复制到剪贴板。我很清楚这个答案,但它已经过去三年了。自那时以来有什么改变吗?
我想知道是否有办法将文本复制到剪贴板。我很清楚这个答案,但它已经过去三年了。自那时以来有什么改变吗?
试一下这个
function myFunc() {
/* Get the text field */
let copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
input {
display: inline-block;
padding: .60rem;
font-size: 1rem;
color: #495057;
background-color: #f1f1f1;
border: 1px solid #ced4da;
border-radius: .25rem;
}
button {
display: inline-block;
font-weight: 400;
color: #ffffff;
cursor: pointer;
text-align: center;
user-select: none;
background-color: #007bff;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
outline: 0;
}
<!-- The text field -->
<input type="text" id="myInput" value="Some Random Text">
<!-- The button used to copy the text -->
<button type="button" onclick="myFunc()">Copy</button>