javascript 如何使用javascript添加粗体文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32531609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:25:43  来源:igfitidea点击:

How to add bold text using javascript

javascripthtmlcss

提问by nachshon f

I'm trying to add bold text to my website using javascript, but it literly shows the "<b>". How do i fix this?

我正在尝试使用 javascript 向我的网站添加粗体文本,但它确实显示了"<b>". 我该如何解决?

var TextBox = document.getElementById("TextBox");

TextData.textContent = "<b> hello </b>"

I tried using the document.write("");, but it changes the whole website. Any Suggestions?

我尝试使用document.write("");,但它改变了整个网站。有什么建议?

回答by Kristijan Iliev

you can add first the content to textbox like this:

您可以先将内容添加到文本框,如下所示:

TextBox.innerHTML = "Hello";

then add its style like this:

然后像这样添加它的样式:

TextBox.style.fontWeight = 'bold';

You can do it just like this:

你可以这样做:

 TextBox.innerHTML = "<b>Hello</b>";

I tend to set style separately. You should avoid adding style like this, better use classes.

我倾向于单独设置样式。您应该避免添加这样的样式,最好使用类。

回答by b263

TextBox.innerHTML = "<b> hello </b>"

回答by EricL

Toggle/add a css class on the containing element (TextBox).

在包含元素 (TextBox) 上切换/添加 css 类。

var TextBox = document.getElementById("TextBox");

TextData.className('bold');
.bold {
    font-weight: bold;
}
<div id="TextBox">
    My text.
</div>

Or something like that.

或类似的东西。