假设我有以下 HTML 元素的列表,其中包含一个 Javascript 事件,用于将 HTML 输入类型文本的内容复制到剪贴板:
<input type="text" size="70" value="something1" id="v1">
<button onclick="CopyToClipboard('v1')">Copy command</button>
<input type="text" size="70" value="something2" id="v2">
<button onclick="CopyToClipboard('v2')">Copy command</button>
等等..
如何使用 JavaScript 复制 txt?
我正在尝试调整下面的代码,但我不知道如何将 HTML id 传递给 javascript 代码。我知道 getElementById() 在该上下文中不正确,不应被调用。但我不知道如何将 id 值传递到 Javascript 函数中。
<script>
function CopyToClipboard(myInput) {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select(myInput);
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
</script>
任何帮助将不胜感激。
谢谢
最佳答案
错误在于您使用了 document.getElementById("myInput")
而不是 document.getElementById(myInput)
。
关于javascript将html输入类型id传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56359344/