如何使用 jQuery 将 HTML 转换为文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8537953/
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
How would I convert HTML into text using jQuery?
提问by Krev
I would like to make it so that the children of any element with the "snippet" class are read as text instead of HTML. I am aware that to do this, I would need to change the <
and >
signs so that the HTML page reads them as text. I have been trying to do this but unfortunately, I only have:
我想这样做,以便将具有“片段”类的任何元素的子元素作为文本而不是 HTML 读取。我知道要做到这一点,我需要更改<
和>
标志,以便 HTML 页面将它们作为文本读取。我一直在尝试这样做,但不幸的是,我只有:
function(){
$('.snippet')
}
回答by Jasper
You can use jQuery's .text()
function which will remove HTML tags:
您可以使用 jQuery 的.text()
函数来删除 HTML 标签:
var text_only = $('.snippet').text();
Here is a demonstration: http://jsfiddle.net/meZjw/
这是一个演示:http: //jsfiddle.net/meZjw/
Docs for .text()
: http://api.jquery.com/text
文档.text()
:http: //api.jquery.com/text
UPDATE
更新
Sime Vidas has a good point, you can iterate through the different .snippet
elements changing their HTML structure one at a time:
Sime Vidas 有一个很好的观点,您可以遍历不同的.snippet
元素,一次一个更改它们的 HTML 结构:
$.each($('.snippet'), function (index, obj) {
var $this = $(this);
$this.html($this.text());
});
Here is a demo using $.each()
: http://jsfiddle.net/meZjw/1/
这是一个使用的演示$.each()
:http: //jsfiddle.net/meZjw/1/
UPDATE
更新
Aepheus has a good point, I don't know if this is what is being asked but you can make a function that will escape HTML entities like in other languages:
Aepheus 有一个很好的观点,我不知道这是否是被问到的问题,但您可以创建一个函数来转义其他语言中的 HTML 实体:
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
Here is demo: http://jsfiddle.net/meZjw/2/
这是演示:http: //jsfiddle.net/meZjw/2/
UPDATE
更新
You can also use .text()
and .html()
in the opposite order as my above example, to the effect of showing the HTML of an element as plain-text:
您还可以按照与上面示例相反的顺序使用.text()
和.html()
,以将元素的 HTML 显示为纯文本:
$.each($('.snippet'), function (index, obj) {
var $this = $(this);
$this.text($this.html());
});
Here is a demo: http://jsfiddle.net/meZjw/31/
这是一个演示:http: //jsfiddle.net/meZjw/31/
回答by Blender
This should work:
这应该有效:
$('.snippet').each(function() {
$(this).text($(this).html());
});
Live demo:http://jsfiddle.net/RrUAA/1/
现场演示:http : //jsfiddle.net/RrUAA/1/
回答by andlrc
Maybe you want your code to be shown? http://jsfiddle.net/vVgvt/4/
也许您希望显示您的代码? http://jsfiddle.net/vVgvt/4/
$('.snippet').html().replace(/</g, "<").replace(/>/g, ">");
回答by Alexx Roche
Thisis what I use:
这是我使用的:
<span class="snippet">Who me? Why thank you.</span>
<div id="show_text"></div>
<script type="text/javascript" charset="utf-8">
(function($) {
var data = $('.snippet').text(); // or how ever you collect the data Object
$('div#show_text').append( decodeURIComponent(jQuery.param(data)) );
// debug report:
if($.browser.mozilla){ //If you talk the talk, then you should toSource()
console.log(data.toSource());
}else{
console.log(data);
}
})(jQuery);
</script>
cut out the parts that you need.
剪下你需要的部分。
回答by Bassam Mehanni
try this
尝试这个
$('.snippet').text($('.snippet').html());