Javascript代码,未终止的字符串文字 [英] Javascript code, unterminated string literal
问题描述
错误控制台:
未终止字符串文字
Error console: unterminated string literal
$html='<li><div class="above">'+$question_number+ 'Question Title</div>
JQuery代码是:
The JQuery code is:
$html='<li><div class="above">'+$question_number+ 'Question Title</div>
<div class="middle"> <input type="text" name="question'+$question_number+ '" size="55"/></div>
<div class="below">'+$question_number+ ' <input type="text" name="option'+$question_number+ '" size="6"/>'+$question_number+ '<input type="text" name="option'+$question_number+ '" size="6"/>
<input class="btn" type="button" name="Submit" value="Add" />
<input class="btn" type="button" name="Submit" value="Remove" />
</div>
</li>';
我知道$ html的值很长,但我怎么能逃脱未终止字符串的陷阱文字?有没有更好的解决这个问题?
I know the value of $html is long, but how can I escape the trap of "unterminated string literal"? Is there a better to work around this problem?
推荐答案
如果你要运行字符串,你的行需要延续字符多行文字。在每行的末尾添加\
字符或使用字符串连接。换句话说,你可能会变错:
Your lines need the continuation character if you're going to run the string literals over multiple lines. Put a "\"
character at the end of each line or use string concatenation. In other words, you could turn the incorrect:
$html='<li><div class="above">' + $question_number + 'Question Title</div>
<div class="middle"> ... ';
进入:
$html='<li><div class="above">' + $question_number + 'Question Title</div>\
<div class="middle"> ... ';
或:
$html='<li><div class="above">' + $question_number + 'Question Title</div>' +
' <div class="middle"> ... ';
你实际上 你的HTML格式不是很好,但是,如果你真的想要它可以打印得很好的形式,你也可以在其中添加嵌入的换行符:
You don't actually need your HTML to be nicely formatted but, if you really want it in a form that you can print nicely, you can put embedded newlines into it as well:
$html='<li><div class="above">' + $question_number + 'Question Title</div>\n' +
'<div class="middle"> ... ';
为了便于阅读,我会使用以下内容:
For readability of the code, I would use something like:
$html = \
'<li>' +
' <div class="above">' + $question_number + 'Question Title</div>' +
' <div class="middle">' +
' <input type="text" name="question' + $question_number+ '" size="55"/>' +
' </div>' +
' <div class="below">' + $question_number +
' <input type="text" name="option' + $question_number +
' " size="6"/>' + $question_number +
' <input type="text" name="option' + $question_number + '" size="6"/>' +
' <input class="btn" type="button" name="Submit" value="Add" />' +
' <input class="btn" type="button" name="Submit" value="Remove" />' +
' </div>' +
'</li>';
然后将其传递给压缩器以获得更短的版本以供分发。
and then pass it through a compressor to get a much shorter version for distribution.
这篇关于Javascript代码,未终止的字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!