有时候后台返回的数据中有字符串,并需要将字符串转化为HTML,下面封装了一个方法,如下

// html转义
function htmlspecialchars_decode(string, quote_style) {
var optTemp = 0
, i = 0
, noquotes = false;
if (typeof quote_style === 'undefined') {
quote_style = 2;
}
string = string.toString().replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE': 1,
'ENT_HTML_QUOTE_DOUBLE': 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE': 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') {
quote_style = [].concat(quote_style);
for (i = 0; i < quote_style.length; i++) {
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
} else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/&#0*39;/g, "'");
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
string = string.replace(/&/g, '&');
return string;
}

  为了便于更好的使用该方法,下面是关于转义为html的案例,如下

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="texts"> </div>
//点击“请求”按钮,开始执行转义并展示
<button class="btns">请求数据</button>
<div class="containerBox"></div>
</body>
<script src="vote/js/jquery_v3.3.1.js"></script>
<script>
$(function(){
$.ajax({
url: '1.json',
type: 'post',
})
.done(function(res) {
console.log(res.data[0]);
var aaa = res.data[0].sas;
var ddd = $('.texts').text(aaa)
htmlspecialchars_decode(res.data[0].sas) })
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
}); }) $('.btns').click(function() {
getReq();
});
function getReq(){
$.ajax({
url: '1.json',
type: 'post',
success:function(res){
console.log(res.data);
$.each(res.data, function(i, values) {
console.log(i)
console.log(values.sas);
var objHtml = htmlspecialchars_decode(values.sas)
$('.containerBox').append(objHtml)
});
},
error:function(e){
console.log(error)
}
}); }
// html转义,调用的时候只需要传第二个值即可
function htmlspecialchars_decode(string, quote_style) {
var optTemp = 0
, i = 0
, noquotes = false;
if (typeof quote_style === 'undefined') {
quote_style = 2;
}
string = string.toString().replace(/</g, '<').replace(/>/g, '>');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE': 1,
'ENT_HTML_QUOTE_DOUBLE': 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE': 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') {
quote_style = [].concat(quote_style);
for (i = 0; i < quote_style.length; i++) {
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
} else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/&#0*39;/g, "'");
}
if (!noquotes) {
string = string.replace(/"/g, '"');
}
string = string.replace(/&/g, '&');
return string;
}
</script>
</html>

  里面的json数据为

 {
"data":[{
"sas":"&lt;section data-id=&quot;23210&quot; class=&quot;xmyeditor&quot; data-type=&quot;lspecial06&quot; data-tools=&quot;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;text-align: center;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size:80px;&quot;&gt; &lt;strong class=&quot;autosort&quot;&gt;1&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;text-align: center;margin-top: -45px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;margin-top: 5px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #333;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 15px;&quot;&gt; &lt;strong class=&quot;xmybrush&quot;&gt;五月&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;color: #666;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 17px;&quot; class=&quot;xmybrush&quot;&gt; 劳动节 &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt;"
},{
"sas":"&lt;section data-id=&quot;23210&quot; class=&quot;xmyeditor&quot; data-type=&quot;lspecial06&quot; data-tools=&quot;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;text-align: center;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size:80px;&quot;&gt; &lt;strong class=&quot;autosort&quot;&gt;1&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;text-align: center;margin-top: -45px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;margin-top: 5px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #333;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 15px;&quot;&gt; &lt;strong class=&quot;xmybrush&quot;&gt;六月&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;color: #666;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 17px;&quot; class=&quot;xmybrush&quot;&gt; 儿童节 &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt;"
},{
"sas":"&lt;section data-id=&quot;23210&quot; class=&quot;xmyeditor&quot; data-type=&quot;lspecial06&quot; data-tools=&quot;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;text-align: center;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #fff;line-height: 80px;text-shadow: 0 1px #ccc, 1px 0 #ccc, -1px 0 #ccc, 0 -1px #ccc;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size:80px;&quot;&gt; &lt;strong class=&quot;autosort&quot;&gt;1&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;text-align: center;margin-top: -45px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;background: url(http://alstyle.xmyeditor.com/sucai-png/20190124/5c498d915285ffwwwdwjjcz.qxwrkofsmliqxrpznudguodmwtvlmj);background-repeat: no-repeat;background-size: 100%;width: 100px;height: 30px;display: inline-block;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;margin-top: 5px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;section style=&quot;color: #333;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 15px;&quot;&gt; &lt;strong class=&quot;xmybrush&quot;&gt;七月&lt;/strong&gt; &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;section style=&quot;color: #666;letter-spacing: 1px;line-height: 24px;&quot; powered-by=&quot;xmyeditor.com&quot;&gt; &lt;p style=&quot;padding: 0px;margin: 0px;font-size: 17px;&quot; class=&quot;xmybrush&quot;&gt; 建军节 &lt;/p&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt;"
}
]
}

直接运行即可显示转义为HTML的布局

字符串转义为HTML的更多相关文章

  1. 是否采用Sybase形式的自动字符串转义(用 '' 表示 ')

    ;; 关于php.ini ;; ; 这个文件必须命名为'php.ini'并放置在httpd.conf中PHPINIDir指令指定的目录中. ; 最新版本的php.ini可以在下面两个位置查看: ; h ...

  2. python字符串转义与正则表达式特殊字符转义

    最近在自学python,字符串和正则表达式的特殊字符转义有点混淆,做个笔记简单总结一下. 1.普通字符串转义 在字符串中使用特殊字符时,要用反斜杠(\)转义字符.例如:'Let\'s go!',这里对 ...

  3. PHP字符串转义

    与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3.addslashes()和stripslashes() 4.mysql ...

  4. 从 RegExp 构造器看 JS 字符串转义设计

    多年前我第一次入职腾讯的时候,DC 从杭州给我寄来了一本他刚翻译出炉的<高性能 JavaScript>.那段时间为了帮忙校对,我仔细阅读了书中的每一个段落,结果积累了不少 JavaScri ...

  5. java字符串转义,把&lt;&gt;转换成<>等字符【原】

    java字符串转义,把<>转换成<>等字符 使用的是commons-lang3-3.4 中的StringEscapeUtils类 package test; import ja ...

  6. react将字符串转义成html语句

    在使用reactjs库的时候,会遇到将一段html的字符串,然后要将它插入页面中以html的形式展现,然而直接插入的话页面显示的就是这段字符串,而不会进行转义,可以用以下方法插入,便可以html的形式 ...

  7. mybatis字符串转义问题

    问题描述 @Select("select * from account order by #{orderBy} #{orderRule} limit #{start},#{offset}&q ...

  8. PHP实现字符串转义和还原

    首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...

  9. PHP怎么实现字符串转义和还原?

    首先大家可以简单了解下什么是转义字符?有什么用? 转义字符是一种特殊的字符常量.转义字符以反斜线"\"开头,后跟一个或几个字符.转义字符具有特定的含义,不同于字符原有的意义,故称“ ...

随机推荐

  1. JDK8源码阅读之Collection及相关方法

    最近面试总会被问到JDK8中的一些新特性,所以闲下来抽时间看了一下8的源码,目前主要看的是数据结构部分,特此记录一下. 新增函数式接口,实现该接口的可以直接用lambda表达式. default和st ...

  2. Flask框架搭建一个日程表

    目录 前言 项目介绍 技术栈 Flask Web开发流程 一.搭建环境 1.1: 创建虚拟环境 1.2: 安装依赖包 1.3: 创建依赖包列表文件 1.4: 测试hello word 二.应用程序开发 ...

  3. 开发一个项目之代码规范ESLint

    ESLint{ "rules": { "semi": ["error", "always"], } }error lev ...

  4. ps切图技巧

    步骤1: ps打开psd文件 步骤2: 点击移动工具,观察左上角的自动选择是否有勾选 ,如果没有最好勾选,对应的选项有图层和组,善于切换这个功能能够有效快速的找到你要的区域 步骤3: 找到要切图的元素 ...

  5. 内核模块中filp->open对文件的读写【转】

    转自:http://guiltcool.blog.chinaunix.net/uid-9950859-id-98917.html 平时网络部分的东西碰的多些,这块一开始还真不知道怎么写,因为肯定和在用 ...

  6. MySQL数据优化

    很多企业,可能每天应对的数据量达百万,千万,甚至上亿的访问量,这样的量已经超过普通配置的mysql所承受的量,所以为了应对日益增长的访问量,我们需要对mysql做出相应的对策,进一步优化mysql以达 ...

  7. R语言︱LDA主题模型——最优主题...

    R语言︱LDA主题模型——最优主题...:https://blog.csdn.net/sinat_26917383/article/details/51547298#comments

  8. oracle Data Modeler 使用教程

    由于 powerdesigner 的版权问题.公司要求集体换成 oracle Data Modeler .免费版就够用,哈哈.这有很详细的入门教程,看一看吧: 官方正版教程 ,特详细,只是英文的,也只 ...

  9. https请求之绕过证书安全校验相关配置

    需在weblogic服务器上配置内存溢出的地方加入一行配置: -DUseSunHttpHandler=true      注:空格隔开 然后调用工具类:https://www.cnblogs.com/ ...

  10. clearTimeout方法在IE上的兼容问题

    今天在修改公司项目的bug时发现一个问题,出错代码如下: clearTimeout(); setTimeout(function(){ // 具体业务逻辑 },100); 这段代码在chrome.fi ...