javascript - How to output the newline character in a string in html
为情所困
为情所困 2017-06-14 10:51:21
0
6
1014

I obtain a string in the background and output it to the

tag in the foreground, but there is a newline character 'n' in the string, and I want to output the newline effect in the same way. How should I deal with this string? I replaced 'n' with
but it still didn't work, and it was output as is.
thank you all

为情所困
为情所困

reply all (6)
女神的闺蜜爱上我

There are two ways, one is to setwhite-space: pre;, the white space will be retained by the browser. Or use

 我是换行字 符串 

, the pre element can define pre-formatted text. Text enclosed in a pre element usually preserves whitespace and newlines.

    学霸

    Isn’t the

    line break charactern?

    If you are in this situation, change it to

      阿神

      According to your description, you should need thepretag.

        女神的闺蜜爱上我

        In js, you can use. innerHTML = 'It
        works'
        to output html; if it is jQuery, use$.fn.html('It
        works')
        !
        ==== I thought it was PHP output
        Use the htmlentities function to output the replaced content!

          阿神

          This is possible. It’s best for the questioner to post the code to know the reason

            仅有的幸福

            nvalid in pre

            pre.innerHTML = 'hey,\nman' // hey, // man

            can be passed in p viareplace

            let str = 'hey,\nman' str = str.replace(/\n/, '
            ') p.innerHTML = str // hey, // man
              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!