Concatenate strings on multiple lines in JavaScript
This question already has an answer here:
Try:
var css = '.myclass { text-align: center .... };' +
'.myclass { something else .... };';
Or:
var css = [
'.myclass { text-align: center .... };',
'.myclass { something else .... };'
].join("n");
Yes, if you are fine with only evergreen browsers you can use the new `
quotes:
var css = `
.multiple {
lines: can;
go: here;
}
`;
If you need to support non-evergreen browsers you can use one of several multi-line string methods:
// Escape new lines (slash must be the *last* character or it will break
var css = '
.multiple {
lines: can;
go: here;
}';
// Use comments and `toString` parsing:
var css = extractMultiLineString(function() {/*
.multiple {
lines: can;
go: here;
}
*/});
// Not tested, but this is the general idea
function extractMultiLineString(f) {
var wrappedString = f.toString();
var startIndex = wrappedString.indexOf('/*') + 2;
var endIndex = wrappedString.lastIndexOf('*/') - 1;
return wrappedString.slice(startIndex, endIndex);
}
您可以使用+=
运算符连接到一个变量。
css = '.myclass { text-align: center .... };'
css += '.myclass { something else .... };';
链接地址: http://www.djcxy.com/p/30442.html
上一篇: 为什么我不能将多行值存储到JavaScript文字中?
下一篇: 在JavaScript中多行连接字符串