最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bolding a word inside innerText - Stack Overflow

programmeradmin0浏览0评论

I am trying to bold a few words inside a paragraph. So far I tried using HTML tags before realizing that doesn't work. Is there a way to do this without getting the element?

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

I am trying to bold a few words inside a paragraph. So far I tried using HTML tags before realizing that doesn't work. Is there a way to do this without getting the element?

var text2_2 = document.createElement("p");
text2_2.className = "reasoning";
text2_2.innerText = "All vitamins are <strong>required</strong> by our ..."
Share Improve this question edited Feb 16, 2018 at 2:50 ekim420 asked Feb 16, 2018 at 2:47 ekim420ekim420 4652 gold badges6 silver badges21 bronze badges 2
  • 5 Use innerHTML not innerText – j08691 Commented Feb 16, 2018 at 2:51
  • 2 @j08691 what a simple solution. Thank you very much. – ekim420 Commented Feb 16, 2018 at 2:52
Add a ment  | 

1 Answer 1

Reset to default 5

Since the string contains some HTML in it and to get the effect of that use innerHTML instead of 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);

发布评论

评论列表(0)

  1. 暂无评论