そのようにレンダリングされる中央に連続した空白がある文字列「レビュー1」を作成する方法。以下の例をご覧ください。
<div id="target"></div>
var name = "Review 1"
$('#target').html(name);
ありがとう
すべてのスペースに1つ使用
複数のスペースが必要な場合は、スペースにhtmlエンコード文字を使用できます:
(=非ブレークスペース)
var name = "Review 1"
var name = "Review 1".replace(/ /g, " ");
$('#target').html( name );
使用する必要があります.replace()
これを試してみてください:
$(function () {
var name = "Review 1"
$('#target').html(name.replace(/\s/g, ' '));
});
実例: http: //jsfiddle.net/t4jtJ/1/
他の人が言っているように使用してください。テキストが動的である場合は、replaceを使用します。
name = name.replace(/\s/g,' ');
var name = "Review 1"
name.split(' ').join(' ');
$('#target').html(name);
さてあなたは<pre>
タグを使うことができます:
$('#target').html('<pre>'+name+'</pre>');
フィドルをチェックアウト:http://jsfiddle.net/8BJuB/