javascript处理HTML的Encode(转码)和Decode(解码)总结
2016-04-29 11:53
716 查看
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式
测试:
运行结果:
测试代码:
测试结果:
来自:
一、用浏览器内部转换器实现转换
1.1.用浏览器内部转换器实现html转码
首先动态创建一个容器标签元素,如DIV,然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持),最后返回这个元素的innerHTML,即得到经过HTML编码转换的字符串了。1.2.用浏览器内部转换器实现html解码
首先动态创建一个容器标签元素,如DIV,然后将要转换的字符串设置为这个元素的innerHTML(ie,火狐,google都支持),最后返回这个元素的innerText(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。1.3.具体实现代码
var HtmlUtil = { /*1.用浏览器内部转换器实现html转码*/ htmlEncode:function (html){ //1.首先动态创建一个容器标签元素,如DIV var temp = document.createElement ("div"); //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持) (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(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。 var output = temp.innerText || temp.textContent; temp = null; return output; } };
测试:
var html = "<br>aaaaaa<p>bbbb</p>"; var encodeHtml = HtmlUtil.htmlEncode(html); alert("encodeHtml:" + encodeHtml); var decodeHtml = HtmlUtil.htmlDecode(encodeHtml); alert("decodeHtml:" + decodeHtml);
运行结果:
二、用正则表达式进行转换处理
使用正则表达式也是一种常用的处理方式,实现原理就是使用替换的方式来实现转码和解码,转码时把<>,空格符,&,',""替换成html编码,解码就把html编码替换成对应的字符,实现代码如下:var HtmlUtil = { /*1.用正则表达式实现html转码*/ htmlEncodeByRegExp:function (str){ var s = ""; if(str.length == 0) return ""; s = str.replace(/&/g,"&"); s = s.replace(/</g,"<"); s = s.replace(/>/g,">"); s = s.replace(/ /g," "); s = s.replace(/\'/g,"'"); s = s.replace(/\"/g,"""); return s; }, /*2.用正则表达式实现html解码*/ htmlDecodeByRegExp:function (str){ var s = ""; if(str.length == 0) return ""; s = str.replace(/&/g,"&"); s = s.replace(/</g,"<"); s = s.replace(/>/g,">"); s = s.replace(/ /g," "); s = s.replace(/'/g,"\'"); s = s.replace(/"/g,"\""); return s; } };
测试代码:
var html = "<br>ccccc<p>aaaaa</p>"; var encodeHTML = HtmlUtil.htmlEncodeByRegExp(html); alert("用正则表达式进行html转码,encodeHTML:" + encodeHTML); var decodeHTML = HtmlUtil.htmlDecodeByRegExp("用正则表达式进行html解码:" + encodeHTML); alert(decodeHTML);
测试结果:
三、封装HtmlUtil工具类
将两种方式封装HtmlUtil工具类,方便在开发中使用,完整代码如下:var HtmlUtil = { /*1.用浏览器内部转换器实现html转码*/ htmlEncode:function (html){ //1.首先动态创建一个容器标签元素,如DIV var temp = document.createElement ("div"); //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐,google支持) (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(ie支持)或者textContent(火狐,google支持),即得到经过HTML解码的字符串了。 var output = temp.innerText || temp.textContent; temp = null; return output; }, /*3.用正则表达式实现html转码*/ htmlEncodeByRegExp:function (str){ var s = ""; if(str.length == 0) return ""; s = str.replace(/&/g,"&"); s = s.replace(/</g,"<"); s = s.replace(/>/g,">"); s = s.replace(/ /g," "); s = s.replace(/\'/g,"'"); s = s.replace(/\"/g,"""); return s; }, /*4.用正则表达式实现html解码*/ htmlDecodeByRegExp:function (str){ var s = ""; if(str.length == 0) return ""; s = str.replace(/&/g,"&"); s = s.replace(/</g,"<"); s = s.replace(/>/g,">"); s = s.replace(/ /g," "); s = s.replace(/'/g,"\'"); s = s.replace(/"/g,"\""); return s; } };
来自:
/article/4676438.html
相关文章推荐
- Simple JSON开发指南
- 160428、JavaScript知识总结—cookie及其应用
- 160426、JavaScript 秘密花园
- JS 图片设置时间遍历循环播放!基础,写着玩
- JavaScript时间戳与其格式化
- JavaScript 过滤,压缩重复字符串
- 仅用原生JavaScript手写DOM事件模型
- BabylonJS娱乐场---基于WebGL的H5 3D游戏引擎
- jsp页面 onclick()函数没触发到js脚本
- js类的继承
- 用Javascript(js)进行HTML转义工具(处理特殊字符显示)
- JavaScript数组过滤相同元素
- JavaScript系列之DOM操作
- C# 自定义Json解析工具
- js如何检测打开窗口是否存在的三个方法?
- 详解js中typeof、instanceof与constructor
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js的静态类
- JavaScript调试技巧之:快速定位
- js中利用prototype给类添加方法