在 JS 中添加字符串之前强制换行

Force line break before adding string in JS?

本文关键字:换行 字符串 JS 添加      更新时间:2023-09-26
<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>

正如您可能知道的那样,我创建了一个"项目"文本框和一个按钮,单击该按钮时,会将所述"项目"文本框的内容添加到"列表"文本区域。

我想知道的是如何在添加项目之前添加换行符?

document.getElementById("list").value += "'n" + document.getElementById("item").value;