简体   繁体   中英

Why the browser is ignoring new empty lines inside the textarea in HTML?

When I try to put text that has multiple empty lines inside a textarea in HTML , the browser ignores them and does not display them. My purpose is to allow the user to edit his text while preserving all the new lines.

If I put this:

text a\n\n\ntext b\ntextc  

The browser displays:

text a
text b
text c

My purpose:

text a


text b
text c

我相信您需要为此使用<br>标记: https : //www.w3schools.com/tags/tag_br.asp

This code adds "abc" with two line feeds after each to a text area. This works in Chrome and in Edge. It could be the browser you are using if you don't see the same. And if you are still stuck, using div instead of textarea may work (div seems more friendly to me, I don't know why). And as the other answer suggests, br sometimes works when slash-n does not.

    <script type="text/javascript">
        function btnTest_Click() {
            var d = document.getElementById("txt1");
            d.innerHTML += 'abc\n\n';
        }
    </script>
    <div>
        <textarea id="txt1" rows="10" cols="25"></textarea>
        <input type="button" value="test" onclick="btnTest_Click()" />
    </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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