当javascript在其中设置空格时,javascript会中断

时间:2012-08-29 13:50:32

标签: javascript jquery

  

可能重复:
  How do I break a string across more than one line of code in 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);

1 个答案:

答案 0 :(得分:1)

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

取而代之的是:

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

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