zl_css.js

(function($){
    $.fn.zl_css = function(config){
        if(!this[0]) return;
        config = config===undefined ? new Object() : config;
        config.img_dir = config.img_dir == undefined ? 'imgs' : config.img_dir;
        //判断是否已经解析过了
        var styles = document.getElementsByTagName("style");
        for(var sindex in styles){
            if(styles[sindex].id == 'css_'+this.attr('id')) return;
        }
        
        var html_str = this[0].outerHTML;
        var css_key_re = undefined;
        var css_arr = new Array();
        var re = new RegExp(" class=['"](.*?)['"]","ig");
        var css_config = new Array();
         
        //css 配置
        //a
        css_config["^ap([\d]+)"] = "-moz-opacity:0.$1;-khtml-opacity: 0.$1;opacity: 0.$1";
        css_config["^ap([\d]+)_e"] = "filter:alpha(opacity=$1)";
        //b
        css_config["^bw([\d-]+)"] = "border-$1px";
        css_config["^bwb([\d-]+)"] = "border-bottom-$1px";
        css_config["^bwl([\d-]+)"] = "border-left-$1px";
        css_config["^bwr([\d-]+)"] = "border-right-$1px";
        css_config["^bwt([\d-]+)"] = "border-top-$1px";
        css_config["^bc(\w{3,6})"] = "border-color:#$1";
        css_config["^bss"] = "border-style:solid";
        css_config["^bsd"] = "border-style:dashed";
        
        css_config["^bgi(\w+)"] = "background-image:url("+config.img_dir+"/$1.gif)";
        css_config["^bgi(\w+)_n"] = "background:url("+config.img_dir+"/$1.gif) no-repeat";
        css_config["^bgc([0-9a-f]{3,6})"] = "background-color:#$1";
        css_config["^bgn"] = "background:none";
        //c
        css_config["^cb"] = 'clear:both';
        css_config["^cp"] = 'cursor:pointer';
        
        //display
        css_config["^db$"] = "display:block";
        css_config["^dib$"] = "display:inline-block";
        css_config["^dn$"] = "display:none";
        //line
        css_config["^lh([\d-]+)"] = "line-height:$1px";
        //margin
        css_config["^m([\d-]+)$"] = "margin:$1px";
        css_config["^mt([\d-]+)"] = "margin-top:$1px";
        css_config["^mb([\d-]+)"] = "margin-bottom:$1px";
        css_config["^mr([\d-]+)"] = "margin-right:$1px";
        css_config["^mra"] = "margin-right:auto";
        css_config["^ml([\d-]+)"] = "margin-left:$1px";
        css_config["^mla"] = "margin-left:auto";
        //float
        css_config["^fl"] = 'float:left';
        css_config["^fr"] = 'float:right';
        css_config["^ofh"] = 'overflow:hidden';
        css_config["^ofa"] = 'overflow:auto';
         
        //font
        css_config["^ff1(\d+)"] = "font:$1px/1.7 Arial,'宋体',sans-serif";
        css_config["^fwb"] = "font-weight:bold";
        css_config["^fs(\d+)"] = "font-size:$1px";
        css_config["^c([0-9a-f]{3,6})"] = "color:#$1";
        css_config["^tdn"] = 'text-decoration:none';
        css_config["^tdu"] = 'text-decoration:underline';
        css_config["^ffm"] = "font-family: Microsoft Yahei";
        //padding
        css_config["^p([\d-]+)"] = "padding:$1px";
        css_config["^pt([\d-]+)"] = "padding-top:$1px";
        css_config["^pr([\d-]+)"] = "padding-right:$1px";
        css_config["^pb([\d-]+)"] = "padding-bottom:$1px";
        css_config["^pl([\d-]+)"] = "padding-left:$1px";
        css_config["^pra"] = "padding-right:auto";
        css_config["^pla"] = "padding-left:auto";
         
        css_config["^pf"] = "position:fixed";
         
        //text-align
        css_config["^tal"] = "text-align:left";
        css_config["^tar"] = "text-align:right";
        css_config["^tac"] = "text-align:center";
        //width and height
        css_config["^h([\d-]+)"] = "height:$1px";
        css_config["^h([\d-]+)b"] = "height:$1%";
        css_config["^w([\d-]+)"] = "$1px";
        css_config["^w([\d-]+)b"] = "$1%";
        css_config["^miw([\d-]+)"] = "min-$1px";
        //ul
        css_config["^lstn"] = 'list-style-type:none';
         
        var classes = html_str.match(re);
        for(var key in classes){    
            if(isNaN(key)) continue;
            class_str = classes[key].replace(/s*class=['"](.*?)['"]/g, '$1');
            var class_arr = class_str.split(' ');
            for(var cindex in class_arr){
                for(var css_key in css_config){
                    css_key_re = new RegExp(css_key,"ig");
                     
                    if(css_key_re.test(class_arr[cindex])){
                        var tmp_value = class_arr[cindex];
                        css_arr[class_arr[cindex]] = tmp_value.replace(css_key_re, css_config[css_key]);
                    }
                }
            }
            setTimeout('var a=0', 15);
        }
        //创建style
        var style_nod = document.createElement("style");
        style_nod.type="text/css";
        style_nod.id='css_'+this.attr('id');
        var css_str = '';
        var import_str = '';
        var hover_str = '';
        var ckey_str = '';

        for(var ckey in css_arr){
            import_str = /_i(_|$)/.test(ckey) ? ' !important' : '';
            hover_str = /_h(_|$)/.test(ckey) ? ':hover' : '';
            ckey_str = /^(.*?)_([^_]{2,}.*)$/.test(ckey) ? ckey.replace(/^(.*?)_([^_]{2,}.*)$/, " $2") : '';
            ckey_str = ckey_str.replace(/^(.*?)_(.*?)$/, "$1");
            css_str += '.'+ckey+ckey_str+''+hover_str+'{'+css_arr[ckey].replace(/_h((_.*$)|$)/, "$1").replace(/_i((_.*$)|$)/, "$1").replace(/_.*/, '')+import_str+'}';
        }

        if(style_nod.styleSheet){
            style_nod.styleSheet.cssText = css_str;  
        }else style_nod.innerHTML = css_str; 
        document.getElementsByTagName("head")[0].appendChild(style_nod);   
    }
})(jQuery)
原文地址:https://www.cnblogs.com/likeastone/p/3575463.html