Javascript Javascript解码html实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10715801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript decoding html entities
提问by petko_stankoski
Possible Duplicate:
How to decode HTML entities using jQuery?
可能的重复:
如何使用 jQuery 解码 HTML 实体?
I want to convert this text:
我想转换这个文本:
"<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>"
to html, with tags and everything in Javascript or Jquery. How to do this?
到 html,带有标签和 Javascript 或 Jquery 中的所有内容。这该怎么做?
回答by Darin Dimitrov
var text = '<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>';
var decoded = $('<textarea/>').html(text).text();
alert(decoded);
This sets the innerHTML of a new element (not appended to the page), causing jQuery to decode it into HTML, which is then pulled back out with .text().
这会设置新元素的innerHTML(未附加到页面),导致jQuery 将其解码为HTML,然后使用.text() 将其拉回。
现场演示。
回答by Bojangles
There is a jQuery solution in this thread. Try something like this:
这个线程中有一个 jQuery 解决方案。尝试这样的事情:
var decoded = $("<div/>").html('your string').text();
This sets the innerHTML of a new <div>
element (not appended to the page), causing jQuery to decode it into HTML, which is then pulled back out with .text()
.
这会设置新<div>
元素的 innerHTML (未附加到页面),导致 jQuery 将其解码为 HTML,然后使用.text()
.
回答by VisioN
Using jQuery the easiest will be:
使用 jQuery 最简单的是:
var text = '<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>';
var output = $("<div />").html(text).text();
console.log(output);
回答by Kaidul
I think you are looking for this ?
我想你在找这个?
$('#your_id').html('<p>name</p><p><span style="font-size:xx-small;">ajde</span></p><p><em>da</em></p>').text();