jQuery: Convert TextArea content to html string and vice versa

前端 未结 3 2073
栀梦
栀梦 2021-02-05 08:51

what I\'m trying to do is converting a TextArea content into a valid html code. Suppose you type inside the TextArea and then you press a button that shows the typed text inside

相关标签:
3条回答
  • 2021-02-05 09:24

    Consider looking into a markdown editor. Stack overflow uses a modified version of this one: http://wmd-editor.com/

    0 讨论(0)
  • 2021-02-05 09:30

    Well, it's not very difficult:

    $('#element').html($('textarea').html().replace(/\n/g, "<br />"));
    
    0 讨论(0)
  • 2021-02-05 09:33

    With the appropriate styling on the element, you shouldn't need to convert anything. Using the CSS white-space property with a value set to pre, any white-space in the element should appear exactly as it does in the textarea:

    #myElement { white-space: pre; }
    

    Example: http://jsfiddle.net/TkpSu/

    0 讨论(0)
提交回复
热议问题