简体   繁体   English

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

[英]Force line break before adding string in JS?

<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>

As you may be able to tell, I have created an "Item" textbox and a button that, when clicked, adds the content of said "Item" textbox to the "List" textarea. 如您所知,我创建了一个“项目”文本框和一个按钮,当单击该按钮时,会将所述“项目”文本框的内容添加到“列表”文本区域。

What I want to know is how do I add a line break before it adds the item? 我想知道的是如何在添加项目之前添加换行符?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM