zoukankan      html  css  js  c++  java
  • html转义

    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;
            }
    }
    
    $(function () {
        var textarea = $('#topics').find("textarea")
        textarea.val("")
        textarea.css("resize", "none")
        textarea.css("overflow", "scroll;")
        textarea.css("width", "100%")
        textarea.css("height", "200px")
        $("#encode").click(function () {
            var html = $("#html").val()
            var text = HtmlUtil.htmlEncode(html)
            $("#text").val(text)
        })
        $("#decode").click(function () {
            var text = $("#text").val()
            var html = HtmlUtil.htmlDecode(text)
            $("#html").val(html)
        })
    });
    
    #topics textarea {
        background: #6ce26c1c;
    }
    #cnblogs_post_body button{
        line-height: 24pt;
        padding-right: 11pt;
        text-indent: 9pt;
        text-align : left;
        vertical-align : middle;
        border: #10a05e;
        -moz-appearance : none;
        appearance : none;
        font-size: 12pt;
        color: #f5f5f5;
        outline : none;
        cursor: pointer;
        background: #10a05e;
    }
    
  • 相关阅读:
    关于多态的一些问题
    003 关于shell基础,大数据的前期准备
    002 在大数据中基础的llinux基本命令
    013 MapReduce八股文的wordcount应用
    接口里语句的修饰问题
    Apache Rewrite url重定向功能的简单配置
    学习笔记 --- 缓存、动态页面静态化、网站优化
    使用PHP连接、操纵Memcached的原理和教程
    Apache中关于页面缓存的设置
    缓存(之一) 使用Apache Httpd实现http缓存
  • 原文地址:https://www.cnblogs.com/mldonkey/p/10721201.html
Copyright © 2011-2022 走看看