关于 append <li>
的正确语法有很多结果的,但是我正在尝试找到一个解决方案 +this['name']+
值包含在 <li>
中的。 firebug 正在显示 'SyntaxError: unterminated string literal'
jslint 正在显示 'Unclosed string'
.我已经尝试了逗号位置的许多不同变体,但我无法让它发挥作用。
$.each(data.result, function() {
$("ul").append("<li>Name: "+this['name']+"</li>
<li>Age: "+this['age']+"</li>
<li>Company: "+this['company']+"</li>
<br />");
});
谢谢。
最佳答案
您可以使用反斜杠字符转义行尾 \
,像这样:
$.each(data.result, function(){
$("ul").append("<li>Name: " + this['name'] + "</li> \
<li>Age: " + this['age'] + "</li> \
<li>Company: "+this['company']+"</li> \
<br />");
});
这是因为 Javascript 有时会在行尾自动插入半列。在这种情况下,您的字符串并不接近。另一种解决方案是关闭每行的每个字符串,并使用 +
将它们全部连接起来。
$.each(data.result, function(){
$("ul").append("<li>Name: " + this['name'] + "</li>" +
"<li>Age: " + this['age'] + "</li>" +
"<li>Company: "+this['company']+"</li>" +
"<br />");
});
(无关,但你 <br/>
不允许在 <ul>
元素内)
关于javascript - 如何在 javascript 中连接多行字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205464/