如何使字符串的一部分变为粗体?

How to make a part of string bold?

我有这个 html>select 列表,可以让您快速查看该项目,但有一个特定的 字符串的一部分,我想让一个词加粗但无论如何都不起作用。我在这里遇到过类似的问题,尝试过,但没有用。 你能帮我解决这个问题吗?

这里是这个 switch 语句的具体部分 -

 let str = "BEWARE!";

para.textContent = "a leather rebel,menace crafated glove,that charges up"+
        ", holds an insane amount of energy until it is unleashed anyway you want it to but"+
         str.bold() +" for it may burn you instead if not released in time!";

String.bold() 函数只将您的字符串包装到 标签中。

使用上下文不支持 HTML 标签。

提示:为视觉粗体效果使用 标签是一种“糟糕”的做法。你最好使用 CSS!

您必须使用 innerHTML 而不是 textContent,它将满足您的需要

检查代码段

let str = 'BEWARE!';
let para = document.querySelector('p');
para.innerHTML = "a leather rebel,menace crafated glove,that charges up"+
        ", holds an insane amount of energy until it is unleashed anyway you want it to but " +
         str.bold() +  " for it may burn you instead if not released in time!";
<div>
<p></p>
</div>