如何使用javascript添加粗体文本 [英] How to add bold text using javascript
本文介绍了如何使用javascript添加粗体文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 javascript 向我的网站添加粗体文本,但它确实显示了 "<b>"
.我该如何解决这个问题?
I'm trying to add bold text to my website using javascript, but it literly shows the "<b>"
. How do i fix this?
var TextBox = document.getElementById("TextBox");
TextData.textContent = "<b> hello </b>"
我尝试使用 document.write("");
,但它改变了整个网站.有什么建议吗?
I tried using the document.write("");
, but it changes the whole website. Any Suggestions?
推荐答案
您可以像这样先将内容添加到文本框:
You can add first the content to textbox like this:
TextBox.innerHTML = "Hello";
然后像这样添加它的样式:
then add its style like this:
TextBox.style.fontWeight = 'bold';
或者你可以设置元素的 innerHTML
属性,就像这样:
Or you can do it setting the innerHTML
property of the element, just like this:
TextBox.innerHTML = "<b>Hello</b>";
我倾向于单独设置样式.你应该避免添加这样的样式,最好使用css类.
I tend to set style separately. You should avoid adding style like this, better use css classes.
这篇关于如何使用javascript添加粗体文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文