Newline problem when writing to HTML using JavaScript

后端 未结 4 2180
逝去的感伤
逝去的感伤 2021-01-21 14:03

I\'m using regular textbox as a text input where the users wrties their comments. I then use JQuery and JSON to send data to the server and then insert it into

4条回答
  •  我在风中等你
    2021-01-21 14:45

    Before writing out the HTML using javascript to the page, make sure to replace all the newlines with
    tags. Here is a simple extension for string that will allow you to do it using javascript (source):

    String.prototype.NewlineToBR = function() {
        return this.replace( /\r\n|\r|\n/g, br || '');
    }
    

    Usage:

    var htmlString = newlineString.NewlineToBR();
    

    Then just insert the new string into you HTML.

提交回复
热议问题