2017-03-02 70 views
0

Sample Comment如何修剪起點和終點之間的空間和新線路,更換多個新線條和寬敞的空間與單一的JS

喂配合......我有這可能是你而是很簡單的一個問題我的一個相當混亂...可能是它看起來像一個共同的問題,但我還沒有發現任何情況下,是我的類似...我有一個網站上,用戶可以添加評論的帖子...現在什麼我想要的是,當用戶放置多個空間時,應該將其轉換爲單個空間,並且應該使用多個新行來完成...除此之外,我還希望從開始和結束時剪掉寬空格和新行字符串...查看示例輸入的屏幕截圖的鏈接(圖片)

這裏是我的代碼:

$(document).on('keydown','.addComment',function(e){ 
    var id = $(this).attr('id').match(/\d+/); 
    var p_id = Number(id); 
    var comment_box = '#comment_box_'+id; 
    var content = $(comment_box).html(); 
    content = content.replace(/\n/g, '<br>'); //replacing new line with <br> 
    if (e.which === 13 && e.shiftKey !== true) { 

     if (content.length > 0 ) { 

      $.ajax({ 

       type : 'POST', 
       url : 'update.php', 
       data: ({ 
        content: content, 
        id: p_id, 
        act: "add_cmnt" 
       }), 
       success : function() 
       { 
        update_ca("comment_area_"+id, true); 
       } 
      }); //End of Ajax 
     } 
     return false; 
     } 
     }); 

這裏是HTML的輸入代碼:

<p contenteditable="true" class=" addComment" id="comment_box_***"></p> 

回答

0

你可以試試這個代碼:

content = content.replace(/\n+/gm, '\n').replace(/\u0020+/gm, ' ').replace(/\n/g, '<br>'); 

最好的問候。

+0

它根本不工作... – Shehzad

相關問題