在JavaScript中多行连接字符串
这个问题在这里已经有了答案:
尝试:
var css = '.myclass { text-align: center .... };' +
'.myclass { something else .... };';
要么:
var css = [
'.myclass { text-align: center .... };',
'.myclass { something else .... };'
].join("n");
是的,如果你只用常绿的浏览器罚款,你可以使用新的`
报价:
var css = `
.multiple {
lines: can;
go: here;
}
`;
如果您需要支持非常规浏览器,则可以使用以下几种多行字符串方法之一:
// 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/30441.html
上一篇: Concatenate strings on multiple lines in JavaScript
下一篇: Alternative of triple quoted string in Javascript as Python has