如何使用 jQuery 解码 HTML 实体? [英] How to decode HTML entities using jQuery?

查看:51
本文介绍了如何使用 jQuery 解码 HTML 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 jQuery 解码字符串中的 HTML 实体?

How do I use jQuery to decode HTML entities in a string?

推荐答案

安全说明:使用这个答案(保留在下面的原始形式)可能会引入一个XSS 漏洞进入您的应用程序.您不应使用此答案.阅读lucascaro 的答案以了解此答案中的漏洞,并使用该答案中的方法或 Mark Amery 的答案.

Security note: using this answer (preserved in its original form below) may introduce an XSS vulnerability into your application. You should not use this answer. Read lucascaro's answer for an explanation of the vulnerabilities in this answer, and use the approach from either that answer or Mark Amery's answer instead.

其实试试

var encodedStr = "This is fun & stuff";
var decoded = $("<div/>").html(encodedStr).text();
console.log(decoded);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div/>

这篇关于如何使用 jQuery 解码 HTML 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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