简体   繁体   中英

How to output html tag as part of string content to div using JQuery

I have variable "htmlElement" which has the format like <p class="Day">Test Message</p>

What I am going to do is to output the content of htmlElement as a string to the div which has a class panel-body.

What I did is $(".panel-body").append(htmlElement);

But in the div.panel-body, user can only see Test Message , the other part has been treated as html tag.

The question is , how could I let the div.panel-body show "<p class=Day>Test Message</p>"

Thanks.

Well, I don't know what you have stored in variables already, but if you have <p class=Day>Test Message</p> stored in a string variable, you can do this:

$(".panel-body").text(variable);

(As Musa said, this replaces the content.)

To add, do:

$(".panel-body").append("<p class=Day>Test Message</p>");

or

$(".panel-body").prepend("<p class=Day>Test Message</p>");

How about

$(".panel-body").append(document.createTextNode(htmlElement));

http://jsfiddle.net/9z3zE/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM