将innerText内的单词加粗

elb*_*kim 1 html javascript css dom

我试图将段落中的几个单词加粗。到目前为止,我尝试过使用 HTML 标签,然后才意识到这不起作用。有没有办法在不获取元素的情况下做到这一点?

var text2_2 = document.createElement("p");
text2_2.className = "reasoning";
text2_2.innerText = "All vitamins are <strong>required</strong> by our ..."
Run Code Online (Sandbox Code Playgroud)

Mam*_*mun 5

由于字符串中包含一些 HTML,为了获得该效果,请使用innerHTML而不是innerText.

var text2_2 = document.createElement("p");
text2_2.className = "reasoning";
text2_2.innerHTML = "All vitamins are <strong>required</strong> by our ..."

document.body.appendChild(text2_2);
Run Code Online (Sandbox Code Playgroud)

  • 我不知道innerHTML。谢谢。 (2认同)