节点JS的新行

时间:2018-08-09 11:21:33

标签: node.js line-breaks

我一直在尝试找出将\ r \ n放置在代码中的位置。下面的代码为我提供了

的输出
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");
}

}

1 个答案:

答案 0 :(得分:0)

不需要使用forEach来连接unique数组中的字符串。您可以改用join,它使您可以指定放置在两者之间的“胶水”字符串(在您的情况下为换行符):

var unique = document.querySelectorAll('.unique');
var msg = unique.join("\r\n");