下面的HTML代码是一个循环结果.我想要的只是通过单击按钮来复制输入值,但我无法使其工作.
function myFunction() {
// Get the text field
var copyText = document.getElementById("myInput");
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
// Alert the copied text
alert("Copied the text: " + copyText.value);
}
<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>
<input type="text" value="Hello World fdasfds" id="myInput">
<button onclick="myFunction()">Copy text</button>
<input type="text" value="Hello World fdasfd fsdafds" id="myInput">
<button onclick="myFunction()">Copy text</button>
如果只有一个条目,则它可以完美地工作.
function myFunction() {
// Get the text field
var copyText = document.getElementById("myInput");
// Select the text field
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
// Alert the copied text
alert("Copied the text: " + copyText.value);
}
<input type="text" value="Hello World fdasfd fsdafds" id="myInput">
<button onclick="myFunction()">Copy text</button>