使用jQuery获取内部html而不用转义或解码任何东西 [英] Getting inner html without escaping or decoding anything with jQuery
问题描述
我正在开发某种在线html编辑器,并且在使用jQuery获取textarea内容方面存在问题。
Textarea元素如下所示:
< textarea id =myText>< b>& lt; p& gt; TEST& lt; p&安培; GT;< / b>< / textarea的>
所以它是一个转义和非转义文本的混合体。然而,浏览器显示这样的textarea,这也是我不想要的东西:
< b><< p为H.; TEST< / p>< / b个
我想获取textarea的内部html。如果我尝试 .html()
,它会转义所有标记,如果我尝试 .val()
逃跑的标签。你可以在 http://jsfiddle.net/hnBte/ 看到行为。
如何使用Javascript获得纯内容,并且可以选择如何让textarea显示内容?
感谢我发现这是一个非常有趣的问题,所以我做了一些研究:我尝试了所有的JavaScript和jQuery函数,看看我会得到什么,但没有任何工作。你只能得到完全没有解析或完全解析的html。
我也偶然发现了这个SO问题,这也证实了,不幸的是,它不能完成: 使用HTML获取HTML元素的原始文本内容未解释。
I am developing some kind of online html editor and I have a problem with getting the content of a textarea with jQuery.
The Textarea element is like this:
<textarea id="myText"><b><p>TEST</p></b></textarea>
So it is a mixture of escaped and unescaped text. However, the browser shows the textarea like this, which is also something that I don't want :
<b><p>TEST</p></b>
I want to get the inner html of the textarea as it is. If I try .html()
, it escapes all the tags and if I try .val()
, it decodes the escaped tags. You can see the behavior at http://jsfiddle.net/hnBte/
How can I get the pure content with Javascript and optionally, how can I make the textarea show the content as it is?
Thanks
I found this a really interesting question, so I did some research: I tried all javascript and jQuery functions to see what I would get, but nothing worked. You can only get completely unparsed or completely parsed html.
I also stumbled upon this SO-question, that also confirms that, sadly, it just can't be done: Getting raw text content of HTML element with HTML uninterpreted.
这篇关于使用jQuery获取内部html而不用转义或解码任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!