How to make string "Review 1" with sequencial white spaces in the middle to be rendered as such. Please see the example below.
<div id="target"></div>
var name = "Review 1"
$('#target').html(name);
Thanks
每个空间使用一个
您需要使用.replace()
试一试:
$(function () {
var name = "Review 1"
$('#target').html(name.replace(/\s/g, ' '));
});
工作示例:http: //jsfiddle.net/t4jtJ/1/
如果您需要多个空格,您可以使用 html 编码字符作为空格:
(=非换行空格)
var name = "Review 1"
var name = "Review 1".replace(/ /g, " ");
$('#target').html( name );
像其他人说的那样使用。如果文本是动态的,请使用替换:
name = name.replace(/\s/g,' ');
var name = "Review 1"
name.split(' ').join(' ');
$('#target').html(name);