AngularJS textarea newline on enter key

前端 未结 1 744
逝去的感伤
逝去的感伤 2021-01-16 07:18

I am developing an AngularJS application same like how we post answers and questions on stackoverflow and we get live preview of our answers or question just below the texta

相关标签:
1条回答
  • 2021-01-16 08:06

    Thats because newlines in the textarea are not HTML line breaks. You need to replace them with something like this:

    $(document).ready(function() {
      var pad = $('#pad');
      var board = $('#board');
    
      pad.keypress(function(event) {
        board.html(pad.val().replace(/\r?\n/g, '<br />'));
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea id="pad"></textarea>
    <div id="board"></div>

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