首页 > 其他分享 >js-转译

js-转译

时间:2023-11-17 16:45:36浏览次数:27  
标签:function return temp replace html 转译 var js

var HtmlUtil = {
        /*1.用浏览器内部转换器实现html编码(转义)*/
        htmlEncode:function (html){
            //1.首先动态创建一个容器标签元素,如DIV
            var temp = document.createElement ("div");
            //2.然后将要转换的字符串设置为这个元素的innerText或者textContent
            (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
            //3.最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了
            var output = temp.innerHTML;
            temp = null;
            return output;
        },
        /*2.用浏览器内部转换器实现html解码(反转义)*/
        htmlDecode:function (text){
            //1.首先动态创建一个容器标签元素,如DIV
            var temp = document.createElement("div");
            //2.然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持)
            temp.innerHTML = text;
            //3.最后返回这个元素的innerText或者textContent,即得到经过HTML解码的字符串了。
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        },
        /*3.用正则表达式实现html编码(转义)*/
        htmlEncodeByRegExp:function (str){  
             var temp = "";
             if(str.length == 0) return "";
             temp = str.replace(/&/g,"&");
             temp = temp.replace(/</g,"&lt;");
             temp = temp.replace(/>/g,"&gt;");
             temp = temp.replace(/\s/g,"&nbsp;");
             temp = temp.replace(/\'/g,"&#39;");
             temp = temp.replace(/\"/g,"&quot;");
             return temp;
        },
        /*4.用正则表达式实现html解码(反转义)*/
        htmlDecodeByRegExp:function (str){  
             var temp = "";
             if(str.length == 0) return "";
             temp = str.replace(/&amp;/g,"&");
             temp = temp.replace(/&lt;/g,"<");
             temp = temp.replace(/&gt;/g,">");
             temp = temp.replace(/&nbsp;/g," ");
             temp = temp.replace(/&#39;/g,"\'");
             temp = temp.replace(/&quot;/g,"\"");
             return temp;  
        },
        /*5.用正则表达式实现html编码(转义)(另一种写法)*/
        html2Escape:function(sHtml) {
             return sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
        },
        /*6.用正则表达式实现html解码(反转义)(另一种写法)*/
        escape2Html:function (str) {
             var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
             return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
        }
    };


、/////////////////////////////////////////////
encodeURIComponent()
decodeURIComponent()

 

标签:function,return,temp,replace,html,转译,var,js
From: https://www.cnblogs.com/WebMadman/p/12411586.html

相关文章

  • 纯css js 写出星空背景
     每次刷新星星的位置都是随机的,可以根据自己需求调整星星的数量和位置,具体代码如下,直接复制就可运行1<!DOCTYPEhtml>2<html>3<head>4<title>RandomStarrySky</title>5<style>6body{7margin:0;8overflow:......
  • Spring Boot 访问静态资源css/js
    一、前言我们用SpringBoot搭建Web应用时(如搭建一个博客),经常需要在Html中访问一些静态资源,比如:css样式;js脚本;favicon.ico图标等;而在SpringBoot中如果没有做任何配置,是无法直接访问静态资源的,通常会报404错误二、SpringBoot访问静态资源的默认目录SpringBoot访问......
  • 解析json
    result.SetSuccess(Util.TryGetJSONObject<JObject>("{\"obj\":{\"reply\":\""+row.response+"\"},\"code\":"+0+"}")); {"Success":true,"Message&......
  • js内置对象
    Js标准内置对象ObjectObject是JavaScript的一种数据类型。它用于存储各种键值集合和更复杂的实体。可以通过Object()构造函数或者使用对象字面量的方式创建对象。Object.assign()Object.assign()静态方法将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返......
  • flask取消jsonify自动排序
    将此配置行添加到应用程序定义之后的代码中:app=Flask(__name__)app.config['JSON_SORT_KEYS']=False对于Flask2.3及更高版本,请使用以下命令:app.json.sort_keys=False......
  • bulk批量操作的json格式解析
    3.17bulk批量操作的json格式解析bulk的格式:{action:{metadata}}\n{requstbody}\n为什么不使用如下格式:[{"action":{},"data":{}}]这种方式可读性好,但是内部处理就麻烦了:1.将json数组解析为JSONArray对象,在内存中就需要有一份json文本的拷贝,另外还有一个JSONArray对象。2.解析jso......
  • js给多个具有相同class的元素绑定同一个事件或者样式
    <buttonclass="btn1">month1</button><buttonclass="btn1">month2</button><buttonclass="btn1">month3</button>若要给上面3个都具有btn1的class的按钮,添加同样的点击事件,则如下操作:$(".btn1").each(function(){......
  • 用JS实现简单的新闻向上轮播效果
    最近在项目中遇到一个需求,就是实现一个功能,具体内容就是写一个类似轮播的功能,有限条标题,循环轮播。首先准备一个div,里边设置好要展示的内容divclass="panelline1"style="overflow:hidden"><h2>新闻动态</h2><divclass="app"><ahref="https://w......
  • java:Json
    /***encoding:utf-8*版权所有2023涂聚文有限公司*许可信息查看:*描述:*#Author:geovindu,GeovinDu涂聚文.*#IDE:IntelliJIDEA2023.1Java17*#Datetime:2023-2023/11/16-12:29*#User:geovindu*#Product:Int......
  • 【开源】基于Vue.js的计算机机房作业管理系统的设计和实现
    一、摘要1.1项目介绍基于Vue+SpringBoot+MySQL的计算机机房作业管理系统包含课程档案模块、课时档案模块、学生作业模块,还包含系统自带的用户管理、部门管理、角色管理、菜单管理、日志管理、数据字典管理、文件管理、图表展示等基础模块,计算机机房作业管理系统基于角色的访问控制......