简体   繁体   English

如何使用JQuery将html标签作为字符串内容的一部分输出到div

[英]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> 我有一个变量“ htmlElement”,其格式类似于<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. 我要做的是将htmlElement的内容作为字符串输出到具有类panel-body的div。

What I did is $(".panel-body").append(htmlElement); 我所做的是$(".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. 但是在div.panel-body中,用户只能看到Test Message ,另一部分已被视为html标签。

The question is , how could I let the div.panel-body show "<p class=Day>Test Message</p>" 问题是,如何让div.panel-body显示"<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: 好吧,我不知道您已经在变量中存储了什么,但是如果您在字符串变量中存储了<p class=Day>Test Message</p> ,则可以执行以下操作:

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

(As Musa said, this replaces the content.) (正如Musa所说,这取代了内容。)

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/ http://jsfiddle.net/9z3zE/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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