Copying text of textarea into div with line breaks

前端 未结 5 1974
春和景丽
春和景丽 2020-12-31 09:15

I am tying to make a simple effect using keyup() in jQuery. I just want that when user types into the textarea then the text which user types will

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 09:29

    You need to convert the literal newlines into
    tags for proper output in the DIV.

    $('.'+contentAttr+'').html(value.replace(/\r?\n/g,'
    '));

    Shown in your code below:

        $('.content:not(.focus)').keyup(function(){					
                                        
                                        
            var value = $(this).val();
            var contentAttr = $(this).attr('name');
            
            $('.'+contentAttr+'').html(value.replace(/\r?\n/g,'
    ')); //convert newlines into
    tags });
      

     

    Texts Comes here

    JSFiddle

提交回复
热议问题