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

javascript breaks when spaces are set within it

本文关键字:中断 空格 在其中 设置 javascript      更新时间:2023-09-26

可能重复:
如何在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);

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

相反,做一些类似的事情:

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

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