从多个文本框中复制文本的Javascript代码可以使用以下方式实现:
document.getElementById
方法获取每个文本框的值,并将它们拼接成一个字符串。document.execCommand
方法将新创建的文本框中的内容复制到剪贴板中。下面是一个示例的Javascript代码:
// HTML中的文本框
<input type="text" id="text1" value="文本框1的内容">
<input type="text" id="text2" value="文本框2的内容">
<input type="text" id="text3" value="文本框3的内容">
// 点击按钮触发复制操作
<button onclick="copyText()">复制文本</button>
// Javascript代码
function copyText() {
var text1Value = document.getElementById("text1").value;
var text2Value = document.getElementById("text2").value;
var text3Value = document.getElementById("text3").value;
var combinedText = text1Value + "\n" + text2Value + "\n" + text3Value;
var tempInput = document.createElement("textarea");
tempInput.value = combinedText;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
}
此代码将获取每个文本框的值,并将它们拼接为一个带有换行符的字符串。然后,创建一个新的文本框元素,将拼接后的字符串作为其值,并将其添加到页面上。最后,使用document.execCommand("copy")
将新创建的文本框中的内容复制到剪贴板中。
领取专属 10元无门槛券
手把手带您无忧上云