html - Javascript innerHTML ignores Text Format - Stack Overflow

I have following value in table cell with bold & colour formattingcell id="total"; value=

I have following value in table cell with bold & colour formatting

cell id="total"; value="HI"

But when i change the value of the cell as below, it ignores text formatting

document.getElementById("total").innerHTML = "Hello"

and resulting in

Hello (with out bold and color)

I have following value in table cell with bold & colour formatting

cell id="total"; value="HI"

But when i change the value of the cell as below, it ignores text formatting

document.getElementById("total").innerHTML = "Hello"

and resulting in

Hello (with out bold and color)
Share Improve this question asked Aug 18, 2013 at 18:09 loganlogan 8,41638 gold badges122 silver badges188 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

That's because you are replacing the existing content with just "Hello" -- the styling of the previous content was due to properties of the content itself, and that doesn't stick around because that content is wiped out.

If you want styling for your new content you need to provide it manually, for example

document.getElementById("total").innerHTML = "<strong>Hello</strong>";

I think the better solution would be to apply your styles through a stylesheet, as follows:

#total {
    font-weight: bold;
    color: #FF0000; /* Replace this with the right color */
}

Now,

document.getElementById("total").innerHTML = "Hello";

should allow the cell to keep its styles.

Yes the best approach would probably be to use CSS styles.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744312750a4568030.html

相关推荐

  • html - Javascript innerHTML ignores Text Format - Stack Overflow

    I have following value in table cell with bold & colour formattingcell id="total"; value=

    8天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信