0

可能重复:
如何在 JavaScript 中的多行代码中拆分字符串?

嗨,我是 javascript 的新手,我正在努力让它工作。

基本上一切正常,但只要其中有任何空间,它就会中断。

它从数据库中提取一个文本字符串,这很好,直到有如下空格:

var getHtml = '<div class="counterwrap"><div class="countertitle"><h1><?php echo $ublunderconstructioninfo->title ; ?></h1></div><div class="counter"><div class="counternumbers"><div id="counterdays"></div><p>Days</p></div><div class="counternumbers"><div id="counterhours"></div><p>Hours</p></div><div class="counternumbers"><div id="counterminutes"></div><p>Minutes</p></div><div class="counternumberslast"><div id="counterseconds"></div><p>Seconds</p></div></div><div class="countertext">this is where some text goes</div></div>';
            $("body").html(getHtml);

现在,如果有这样的空间,那么它会中断:

    var getHtml = '<div class="counterwrap"><div class="countertitle"><h1><?php echo $ublunderconstructioninfo->title ; ?></h1></div><div class="counter"><div class="counternumbers"><div id="counterdays"></div><p>Days</p></div><div class="counternumbers"><div id="counterhours"></div><p>Hours</p></div><div class="counternumbers"><div id="counterminutes"></div><p>Minutes</p></div><div class="counternumberslast"><div id="counterseconds"></div><p>Seconds</p></div></div><div class="countertext">this is where some text goes

but when there is a linebreak

like this it breaks
</div></div>';
            $("body").html(getHtml);
4

1 回答 1

1

这是因为 javascript 中的字符串不能分成多行。

相反,请执行以下操作:

var SomeString = "line 1\n" + 
   "line 2\n" + 
   "line 3";

或者

var SomeString = "line 1\
    line 2\
    line 3";
于 2012-08-29T13:54:52.607 回答