我有一些代码如下:
var css = '.myclass { text-align: center .... };
约会可以变得很长。
是否有一个var要添加多行然后合并在一起,如:
var
css = '.myclass { text-align: center .... };
+css = '.myclass { something else .... };
那么css是两条线的合并吗?
答案 0 :(得分:6)
尝试:
var css = '.myclass { text-align: center .... };' +
'.myclass { something else .... };';
或者:
var css = [
'.myclass { text-align: center .... };',
'.myclass { something else .... };'
].join("\n");
答案 1 :(得分:3)
是的,如果您只使用常青浏览器,则可以使用新的`
引号:
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);
}
答案 2 :(得分:0)
您可以使用+=
运算符连接变量。
css = '.myclass { text-align: center .... };'
css += '.myclass { something else .... };';
答案 3 :(得分:0)
您可以轻松地写下:
var css = ".myclass { text-align: center;" +
"font-size : 20px;" +
"width: 150px; }";
如果这是你的意思