How do I replace all line breaks in a string with
elements?

前端 未结 13 2325
刺人心
刺人心 2020-11-22 02:00

How can I read the line break from a value with JavaScript and replace all the line breaks with
elements?

Example:

A variable passe

13条回答
  •  旧时难觅i
    2020-11-22 02:28

    Not answering the specific question, but I am sure this will help someone...

    If you have output from PHP that you want to render on a web page using JavaScript (perhaps the result of an Ajax request), and you just want to retain white space and line breaks, consider just enclosing the text inside a

     block:

    var text_with_line_breaks = retrieve_some_text_from_php();
    var element = document.querySelectorAll('#output');
    element.innerHTML = '
    ' + text_with_line_breaks + '
    ';

提交回复
热议问题