源程序:
var LogCss = {
logDiv: {id:'logDiv' ,width:'200px' , height:'220px' , position:'absolute' , top :'200px' ,left: '200px' , border:'1px solid black' , lineHeight:'20px'},
logTitle: {width:'100%' , height:'20px' , background:'black' , color:'white' ,border:'1px solid black' ,fontWeight:'bold' ,fontSize:'12px'} ,
logBody: {close:'both' , width:'100%' , height:'196px' , overflow:'auto'},
logInfo: {width:'auto' , borderBottom:'1px solid #CCCCCC', fontSize:'12px',paddingLeft:'6px'}
}
function Logger(LEVEL){
window['logObj'] = this;
LEVEL = LEVEL || Level.ALL ;
this.level = LEVEL ;
this.logDiv = EAS.createElement('div' , LogCss.logDiv);
var logTitle = EAS.createElement('div' , LogCss.logTitle);
var closeButton = EAS.createElement('img' ,{id:'closeLog' ,width:'16px' ,height:'16px' , float:'right' ,marginLeft:'10px' , cursor:'pointer'});
closeButton.src = 'log_image/close.gif' ;
logTitle.innerHTML = '<div style="width:170px; float:left">javascript 日志生成器</div>' ;
logTitle.appendChild(closeButton);
var logBody = EAS.createElement('div' , LogCss.logBody);
this.logDiv.appendChild(logTitle);
this.logDiv.appendChild(logBody);
document.body.appendChild(this.logDiv);
var lineNum = 0;
this.write = function(msg ,color){
var info = EAS.createElement('div', LogCss.logInfo);
info.style.color = color;
info.innerHTML = (++lineNum) + '、' + msg;
logBody.appendChild(info);
info.onmouseover = function(){
EAS.setStyle(this , {backgroundColor:'#DDDDDD'});
}
info.onmouseout = function(){
EAS.setStyle(this , {backgroundColor:'#FFFFFF'});
}
};
logTitle.onmousedown = startMove;
document.onmousemove = moving ;
logTitle.onmouseup = stopMove;
closeButton.onclick = function(){window['logObj'].logDiv.style.display = "none";}
var startPoint = {};
var isAbleDrag = false;
function startMove(evt){
EAS.setStyle(this , {cursor:'move'});
startPoint = EAS['Event'].getMouseInDocument(evt);
isAbleDrag = true;
}
function moving(evt){
if(!isAbleDrag)return ;
var curPoint = EAS['Event'].getMouseInDocument(evt);
var d_x = curPoint.x - startPoint.x ;
var d_y = curPoint.y - startPoint.y ;
var pos_x = EAS.getPosValue(window['logObj'].logDiv).left + d_x ;
var pos_y = EAS.getPosValue(window['logObj'].logDiv).top + d_y ;
window['logObj'].logDiv.style.left = pos_x + 'px';
window['logObj'].logDiv.style.top = pos_y + 'px';
startPoint = curPoint ;
return false;
}
function stopMove(){
EAS.setStyle(this , {cursor:''});
isAbleDrag = false;
}
}
var Level = {ALL:0 , TRACE:1 , DEBUG:2 ,INFO:3, WARN:4 , ERROR:5 , OFF:6};
var Color = {TRACE:'brown' , DEBUG:'green' ,INFO:'black', WARN:'blue' , ERROR:'red'};
Logger.prototype.setOpacity = function(value /* 0~1 */){
EAS.setOpacity(this.logDiv , value);
}
Logger.prototype.setLevel = function(level){
this.level = level;
}
Logger.prototype.getLevel = function(){
return this.level;
}
Logger.prototype.trace = function(msg){
if(this.level <= Level.TRACE ){
this.write("TRACE:" + msg , Color.TRACE);
return true;
}
return false;
}
Logger.prototype.debug = function(msg){
if(this.level <= Level.DEBUG ){
this.write("DEBUG:" + msg , Color.DEBUG);
return true;
}
return false;
}
Logger.prototype.info = function(msg){
if(this.level <= Level.INFO ){
this.write("INFO:" + msg , Color.INFO);
return true;
}
return false;
}
Logger.prototype.warn = function(msg){
if(this.level <= Level.WARN ){
this.write("WARN:" + msg , Color.WARN);
return true;
}
return false;
}
Logger.prototype.error = function(msg){
if(this.level <= Level.ERROR ){
this.write("ERROR:" + msg ,Color.ERROR);
return true;
}
return false;
}
测试程序如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE>javascript Log test</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type = "text/javascript" src="../eas-base.js"></script>
<script type = "text/javascript" src="log.js"></script>
<style>
html , body{margin:0px; padding:0px;}
</style>
</head>
<body>
<input type="text" id = "input" value="1213"/>
<script>
var log = new Logger(Level.OFF);
log.setLevel(Level.ALL);
log.trace('测试');
log.debug('测试');
log.warn('测试');
log.info('this is a message!');
log.error('测试');
log.error('试试');
log.setOpacity(0.8);
</script>
</body>
</html>
以上程序使用到JS库,有需要的CALL ME!