let word = "kemal"
let test = document.createElement("div");
test.innerText = `lorem la la bla bla "${<strong>word</strong>}"`;
document.querySelector("body").appendChild(test);
我想使粗体字变为可变字体。但我有一个错误。如何在模板文本中使用HTML元素?
您希望使用innerHTML而不是innerText。
test.innerHTML = "lorem la la bla bla <strong>word</strong>";
示例:
let test = document.createElement("div");
test.innerHTML = "lorem la la bla bla <strong>word</strong>";
document.querySelector("body").appendChild(test);
如果要将word
替换为变量:
test.innerHTML = `lorem la la bla bla <strong>${word}</strong>`;
示例:
let word = "Some Word";
let test = document.createElement("div");
test.innerHTML = `lorem la la bla bla <strong>${word}</strong>`;
document.querySelector("body").appendChild(test);