JavaScript:将html文本设为纯文本 [英] JavaScript: make html text plain text
问题描述
我有一个js函数,它将字符串作为参数并将其显示在div元素中.这样的字符串可能包含html标记.
I've got a js-function which takes string as a parameter and get it displayed in a div element. Such string may contain html tags.
如何强制JS将div-elements中的内部文本显示为带有html-tags的html-text.而且,什么是过滤特定标签的适当方法,即将某些标签应用于样式,而仅打印其他标签.
How do I force JS display inner text in div-elements as html-text with html-tags. And, also, what is an adequate way to filter particular tags, i.e. apply certain tags for styling and just print others.
推荐答案
只需将&
和<
(如果需要,可以选择>
,但不一定需要)替换为它们各自的实体,使用String#replace
(规范,
You just need to replace &
and <
(and optionally >
if you like, but you don't have to) with their respective entities, using String#replace
(spec, MDC) for instance.
这篇关于JavaScript:将html文本设为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!