代码分行


<!DOCTYPE html">
<html>
<head>
<title>
</title>
<style>
html,body{font-family:Arial}
div,ol,li{margin-top:0;margin-bottom:0;}
div{border:1px solid red}
li{margin:0;border-left:1px red solid;padding-left:15px;padding:0;color:#860303}
li:hover{background-color:#abcdef}

.red{color:red}
.blue{color:blue}
</style>


<script>
function setContent(msg){
var msgArr = msg.split(/[\r\n]/);
forEach(msgArr, function(msg, i){
var strArr = [], str = "";
for(var i = 0, len = msg.length; i < len; i++){
if(encodeURIComponent(msg.charAt(i)) == "%09"){
strArr.push("    ");
}else if(encodeURIComponent(msg.charAt(i)) == "%20"){
strArr.push(" ");
}else{
var temp = msg.charAt(i).replace(/&/,"&");
temp = temp.replace(/</,"<").replace(/>/,">");
strArr.push(temp);
}
}
forEach(strArr,function(s){
str += s;
})
forEach(redVar,function(v){
str = str.replace(new RegExp(v,"g"),"<span class='red'>"+v+"</span>");
});
forEach(blueVar,function(v){
str = str.replace(new RegExp("\b"+v+"\b","g"),"<span class='blue'>"+v+"</span>");
});
c("li",str,$("ol"));
});
}
var $ = function(id){return document.getElementById(id)},
c = function(tagName,html,p){
var tag = document.createElement(tagName);
tag.innerHTML = html;
app(tag,p);
},
forEach = function(arr,fn){
for(var i = 0, len = arr.length; i < len; i++){
fn.call(this, arr[i], i);
}
},
app = function(c,p){
p.appendChild(c);
},
redVar = ["window","document","location","navigator"],
blueVar = ["var","in","for","function"];

window.onload = function(){
var ta = $("ta");
var btn = $("btn");
btn.onclick = function(){
$("ol").innerHTML = "";
setContent(ta.value);
}
}
</script>


</head>

<body>
<textarea id="ta" cols="66" rows="6">
</textarea>
<button id="btn">setContent</button>
<div>
<ol id="ol">

</ol>
</div>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值