简体   繁体   English

JavaScript无法解码html值

[英]javascript is not decoding html values

I am trying to achieve at below 我正在努力实现以下目标

var a = "how are you. <br> fine";
var b = "how are you, &lt;br&gt; fine";

alert(a);
alert(b);

Output should be 输出应为

How are you 
fine 

But It gives me 但这给了我

how are you. <br> fine
how are you, &lt;br&gt; fine

&gt &lt and &nbsp are in my string &gt&lt和&nbsp在我的字符串中

How can I decode this html in alert message of javascript 如何在javascript的警报消息中解码此html

Ty to use \\n in place of <br> like, 请使用\\n代替<br>

var b = "how are you, \n fine";
alert(b);

Live Demo 现场演示

To show HTML content you can try jquery-alert 要显示HTML content您可以尝试jquery-alert

Try this... 尝试这个...

var text = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';
var decoded = $('<div/>').html(text).text();

alert(decoded);

Edit: As Rohan Kumar has suggested above, I don't think it is possible to render HTML inside an alert box (which is what you are implying in your question). 编辑:正如Rohan Kumar上面建议的那样,我认为不可能在警报框中呈现HTML(这是您在问题中所隐含的含义)。 but rather you would need to do something like below, ie use replace or even better look into using a modal dialogue 但是相反,您将需要执行以下操作,即使用替换或使用 模态对话框 更好地了解

var text = 'how are you, &lt;br&gt; fine';
var text = text.replace('&lt;br&gt;','\n');
var decoded = $('<div/>').html(text).text();

Try it here - DEMO 在这里尝试-演示

alert(decoded); 警报(解码);

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

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