<body>
<center>
<input type="text" id="item">
<button onclick="getItem()">Add to List</button> <br>
<textarea readonly id="list"></textarea>
</body>
<script>
function getItem() {
document.getElementById("list").value += document.getElementById("item").value;
}
</script>
</html>
正如您可能知道的那样,我创建了一个“Item”文本框和一个按钮,单击该按钮会将所述“Item”文本框的内容添加到“List”文本区域。
我想知道的是如何在添加项目之前添加换行符?
最佳答案
document.getElementById("list").value += "\n" + document.getElementById("item").value;
关于javascript - 在JS中添加字符串之前强制换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29471823/