如果您想在实际代码中包含换行符以使其更易于阅读,则需要使用反斜杠对每个换行符进行转义,例如:
var new_comment = '\
<li class="photobooth-comment">\
<span class="username">\
<a href="#">You</a>\
</span>\
<span class="comment-text">\
' + text + '\
</span>\
<span class="comment-time">\
2d\
</span>\
</li>\
';
或者您需要将它们连接为单独的字符串,如下所示:
var new_comment = ''+
'<li class="photobooth-comment">' +
'<span class="username">' +
'<a href="#">You</a>' +
'</span>' +
'<span class="comment-text">' +
text +
'</span>' +
'<span class="comment-time">' +
'2d' +
'</span>' +
'</li>'+
'';
或者简单地把它放在一行上:
var new_comment = '<li class="photobooth-comment"><span class="username"><a href="#">You</a></span><span class="comment-text">' + text + '</span><span class="comment-time">2d</span></li>';
不是那么容易阅读,但对你的 JavaScript 来说更整洁!