Javascript / jQuery - 转换特殊的html字符 [英] Javascript / jQuery - convert special html characters

查看:102
本文介绍了Javascript / jQuery - 转换特殊的html字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些html代码的 pre 元素。
代码中有特殊字符,如& lt; ,所以它不会破坏页面。

I have a pre element with some html code in it. the code has special characters in it, like <, so it doesn't break the page.

然后我有一个javascript函数,它获取这个pre元素的内容,用它加亮(用codemirror),并用突出显示的文本替换元素内容。

Then I have a javascript function that gets the contents of this pre element, highlights it (with codemirror), and replaces the element contents with the highlighted text.

我使用 $(pre)。append(...); 来做到这一点。
问题是,在突出显示后,在屏幕上我看到& lt; 而不是<
如何将这些字符转换回html?

I'm using $("pre").append(...); to do this. The problem is that after the highlighting, on the screen I see &lt; instead of <. How can I convert these characters back to html?

推荐答案

您应该使用.text()方法从pre获取代码。这样你就不会把编码的符号给代码高亮。

You should be using the .text() method to grab the code from the pre. This way you are't giving the encoded symbols to the code highlighter.

这篇关于Javascript / jQuery - 转换特殊的html字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆