合作联系微信: w6668263      合作联系电话:177-9238-7426     

分享javascript来对html编码和解码

    /**
    *htmlEncode编码
    */
    function htmlEncode(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&/g, "&");
        s = s.replace(/</g, "&lt;");
        s = s.replace(/>/g, "&gt;");
        s = s.replace(/\'/g, "&#39;");
        s = s.replace(/\"/g, "&quot;");
        return s;
    }
    /**
    *htmlDecode解码
    */
    function htmlDecode(str) {
        var s = "";
        if (str.length == 0) return "";
        s = str.replace(/&amp;/g, "&");
        s = s.replace(/&lt;/g, "<");
        s = s.replace(/&gt;/g, ">");
        s = s.replace(/&#39;/g, "\'");
        s = s.replace(/&quot;/g, "\"");
        return s;
    } 

 

posted on 2012-12-07 15:56  草率的龙果果  阅读(371)  评论(0编辑  收藏  举报

导航