css如何设置高亮显示,Javascript实现CSS代码高亮显示

Javascript实现CSS代码高亮显示1

/*

*/

body{

font-size:12px;

line-height:1.8;

font-family:'Courier New', Courier, monospace;

}

#area{

width:320px;

height:120px;

}

body{

font-size:12px;

line-height:1.8;

font-family:'微软雅黑';

}

/*

web:http://www.dovapour.com.cn/

mail:dovapour@gmail.com

QQ:362380046

*/

#area{

width:320px;

height:120px;

line-height:1.5;

font-family:"Courier New", Courier, monospace;

}

/*

ul{

margin:0;

padding:0;

list-style:none;

}

table{

border-collapse:collapse;

border-spacing:0;

}

*/

测试

function $(id) {

return document.getElementById(id);

};

$("btn").οnclick=function(){

var code=$("area").value;

//处理注释:注释替换成 _数字_

var startIdx=endIndex=-1;

var at=0;

var commentList=[];

while(true){

startIndex=code.indexOf("/*",at);

if(startIndex==-1)break;

endIndex=code.indexOf("*/",startIndex);

if(endIndex==-1)break;

at=endIndex+2;

commentList.push(code.substring(startIndex,at));

code=code.replace(commentList[commentList.length-1],"_"+(commentList.length-1)+"_");

}

//字符串

code=code.replace(/(['"]).*\1/g,function(m){return ""+m+""});

//CSS样式值

code=code.replace(/:(.+);/g,function(m,n){return ":"+n+";"});

//CSS样式名称

code=code.replace(/[{}]/g,function(m){

if(m=="{"){

return "{";

}else{

return "}";

}

});

//注释

code=code.replace(/_(\d+)_/g,function(m,n){return ""+commentList[n]+""});

//处理\t

code=code.replace(/^(\t+)/gm,function(m){

return (new Array(m.length+1)).join("    ");

});

//处理空格

code=code.replace(/^( +)/gm, function(m) {

return (new Array(m.length + 1)).join(" ");

});

//处理换行

code=code.replace(/\r?\n/g,"
");

$("pre").innerHTML=code;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值