节点 JS 的新行



我一直在试图弄清楚在哪里放置\r以将隔断线放到我的代码中。下面的代码给了我输出

Enter ID:  ID1234Enter Name: John DoeEnter Phone: 12345

我想要的输出是

Enter ID: ID1234
Enter Name: John Doe
Enter Phone: 12345

.HTML

<input type="text "class="unique" size="9" value="Enter ID: " readonly/>
<input type="text "class="unique" size="15" value=" " > <br>
<input type="text "class="unique" size="9" value="Enter Name: " readonly/>
<input type="text "class="unique" size="15" value="" > <br>
<input type="text "class="unique" size="9" value="Enter Phone: " readonly/>
<input type="text "class="unique" size="15" value="" > <br>
<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>

.JS

function doCopy() {
try{
var unique = document.querySelectorAll('.unique');
var msg ="";
unique.forEach(function (unique) {
msg+=unique.value;
});
var temp =document.createElement("textarea");
var tempMsg = document.createTextNode(msg);
temp.appendChild(tempMsg);
document.body.appendChild(temp);
temp.select();
document.execCommand("copy");
document.body.removeChild(temp);
console.log("Success!")
}
catch(err) {
console.log("There was an error copying");
}

}

无需使用forEach来连接unique数组中的字符串。您可以改用 join,它允许您指定一个介于两者之间的"胶水"字符串(在您的情况下,换行符(:

var unique = document.querySelectorAll('.unique');
var msg = unique.join("rn");

最新更新