JS这句话怎么写才正确

How to write this sentence correct JS

我有 2 个变量,我需要将其设为颜色。例如:

table1.style.cssText = "color: textcolor; background-color: bgcolor; "

其中 bgcolor 和 textcolor - 具有颜色值的变量(例如 red/black)

使用模板文字。

table1.style.cssText = `color: ${textcolor}; background-color: ${bgcolor}; "

您可以使用模板文字字符串将变量注入字符串:

const textColor = 'white';
const bgColor = 'black';

table1.style.cssText = `color: ${textColor}; background-color: ${bgColor};`;

演示:

const msg = 'hello';
const msg2 = 'world';

console.log(`${msg} ${msg2}!!!`);

您也可以手动分配值:

const textColor = 'white';
const bgColor = 'black';

table1.style.color = textColor;
table1.style.backgroundColor = bgColor;