2017-04-06 71 views
1

我想顯示在新行文本框邁出了新的行文本輸入,作如下如何在新行中顯示新的行輸入文本?

Hello,       \\1st line 
This is how i want to display,  \\2nd line 
such input is given to textfield. \\3rd line 

我使用下面的代碼通過使用此代碼,什麼文字我正在進入新的生產線就無法顯示在所有。(沒有也第一行文字)這個代碼僅在我使用下面的代碼則顯示輸入文字,但只有一行的所有新訂單如下顯示單行文本

{% for obj in post %} 
<script> 
var str= '{{ obj.content }}'; 
$(".statbox").prepend("<div class='box2'>" + str + "</div>"); 
</script> 
{% endfor%} 

{% for obj in post %} 
    <div class='box2'> {{ obj.content }} </div> 
{% endfor%} 

,其輸出

Hello,\\1st line\\ This is how i not want to display, \\2nd line\\ such input is given to textfield. \\3rd line 

所以,我怎麼能顯示所有新線使用becoz我想DISPLY在prepend方式

Thnaks在進階的文本js代碼照耀處輸入新的行...

回答

1

問題是因爲HTML不尊重空格,除非在某些元素內。

達到你需要什麼,你可以用<br />標記替換字符串中的換行符,就像這樣:

var str = `Hello, 
 
    This is how i want to display, 
 
    such input is given to textfield.`; 
 
$(".statbox").prepend('<div class="box2">' + str.replace(/\n/g, '<br />') + '</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="statbox"></div>

+0

其工作手動指定的字符串,但不適合我。 。在我的情況下,該字符串是在Django對象,即在{{obj.content}}'我在str中的內容,但它不工作... @Rory McCrossan –

+0

這是可能的。你可以編輯你的問題,包括從Django對象輸出的實際JS代碼 –

+0

現在編輯...這是我使用的實際代碼 –

相關問題