复制代码 代码如下:
var lCSSCoder={
format : function(s){//格式化代码
s=s.replace(/\s*([\{\}\:\;\,])\s*/g,"$1");
s=s.replace(/\,[\s\.\#\d]*{/g,"{");
s=s.replace(/([^\s])\{([^\s])/g,"$1 {\n\t$2");
s=s.replace(/([^\s])\}([^\n]*)/g,"$1\n}\n$2");
s=s.replace(/([^\s]);([^\s\}])/g,"$1;\n\t$2");
s=s.replace(/;\s*;/g,";");//清除连续分号
return s;
},
pack :function(s){//压缩代码
s=s.replace(/\/\*(.|\n)*?\*\//g,"");//删除注释
s=s.replace(/\s*([\{\}\:\;\,])\s*/g,"$1");
s=s.replace(/\,[\s\.\#\d]*\{/g,"{");//容错处理
s=s.replace(/;\s*;/g,";");//清除连续分号
return s;
}
};
演示代码:
lCSSCoder: CSS代码压缩与格式化 body {text-align:center;} textarea {border-width:1px;margin:0 5px;} 原始代码: #PageHead {float:left;padding:3px 0;border-bottom:1px solid #b0bec7;margin-bottom:6px;} #hCity li {float:left;padding:0 10px 0 0;} #hCity li.fst {padding:0;} #hRight { float:right;padding:0 5px 0 20px; background:url(img/ico_home.gif) no-repeat 0 50%;} .SubPage #hRight { background-image:url(img/ico_favorite.gif);} #PageBody {float:left;padding:4px 0 0 0;} .SCol {float:left;width:174px;} .MCol {float:right;width:678px;overflow:hidden; } #Logo {height:66px;margin:0;padding:0;} #MailLogin {height:32px;} #MailLogin li {float:left;} #MailLogin li.txt {line-height:20px;} #MailLogin li.inp {padding:0 2px;} #MailLogin li.inp input { margin:0;padding:0 4px;border:1px solid #7a9dbb;font:bold 12px/18px tahoma,Geneva,Arial,sans-serif;height:18px; } #MailLogin li.inp input.notClicked {color:#ccc;font-weight:normal;} #MailLogin li.sel {padding:1px 10px 0 0;} #MailLogin li.btn {padding:1px 12px 0 5px;} 转换后: [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
下面是的css格式化工具,喜欢的朋友也可以看看里面的代码
CSS代码格式化 css在线压缩工具
