javascript 如何在javascript中连接多行字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13205464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I concatenate multiline string in javascript?
提问by user1063287
There are lots of results for the correct syntax for appending <li>
's, however I am trying to find a solution where +this['name']+
values are included in the <li>
's. firebug is displaying 'SyntaxError: unterminated string literal'
and jslint is displaying 'Unclosed string'
. I've tried many different variations of the placements of the commas but I haven't been able to get it to work.
附加<li>
's的正确语法有很多结果,但是我试图找到一个解决方案,其中+this['name']+
值包含在<li>
's 中。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 />");
});
Thank you.
谢谢你。
回答by Simon Boudrias
you can escape end of line with backslash character \
, like so:
您可以使用反斜杠字符转义行尾\
,如下所示:
$.each(data.result, function(){
$("ul").append("<li>Name: " + this['name'] + "</li> \
<li>Age: " + this['age'] + "</li> \
<li>Company: "+this['company']+"</li> \
<br />");
});
This is due to the fact that Javascript automatically insert semi-columns sometime on line end. And in this case, you string weren't close. Another solution is to close each string on each line, and using +
to concat them all.
这是因为 Javascript 有时会在行尾自动插入半列。在这种情况下,您的字符串没有关闭。另一种解决方案是关闭每行上的每个字符串,并使用+
将它们全部连接起来。
$.each(data.result, function(){
$("ul").append("<li>Name: " + this['name'] + "</li>" +
"<li>Age: " + this['age'] + "</li>" +
"<li>Company: "+this['company']+"</li>" +
"<br />");
});
(Unrelated, but you <br/>
aren't allowed inside a <ul>
element)
(无关,但<br/>
不允许在<ul>
元素内部)
回答by Renon Stewart
This should be much faster
这应该快得多
li = '';
$.each(data.result, function(){
li += "<li>Name: " + this['name'] + "</li>" +
"<li>Age: " + this['age'] + "</li>" +
"<li>Company: "+this['company']+"</li>" +
"<br />"; // could remove this and use css
});
$("ul").append(li);
回答by Brad
You actually don't want to concatenate this at all! Consider for a moment what will happen to variable data that contains HTML or HTML-like data. Using your method, it will be parsed as such, possibly breaking things and even opening you up to XSS attack methods.
您实际上根本不想连接它!考虑一下包含 HTML 或类似 HTML 的数据的变量数据会发生什么。使用您的方法,它将被解析为这样,可能会破坏事物,甚至让您接触 XSS 攻击方法。
You're already using jQuery, so the proper way is easy:
您已经在使用 jQuery,因此正确的方法很简单:
$('ul').append(
$('<li/>').text('Name: ' + this.name),
$('<li/>').text('Age: ' + this.age),
// etc.
);
(Note: I believe .append()
allows as many parameters as you give it. If not, try using an array of elements as you append.)
(注意:我相信.append()
允许尽可能多的参数。如果没有,请尝试在追加时使用元素数组。)