Log.js源代码:
function Log() {};
Log.lines = [];
Log.showLog=true;
Log.write = function(s) {
if (this.showLog) {
this.lines.push(s);
this.show();
}
};
Log.clear = function() {
if (this.showLog) {
var l = this.div();
l.innerHTML = '';
this.lines = [];
}
}
Log.show = function() {
var l = this.div();
l.innerHTML += this.lines.join('<br/>') + '<br/>';
this.lines = [];
l.scrollTop = l.scrollHeight;
}
Log.div = function() {
var l = document.getElementById('log');
if (!l) {
l = document.createElement('div');
l.id = 'log';
l.style.position = 'absolute';
l.style.right = '5px';
l.style.top = '5px';
l.style.width = '250px';
l.style.height = '150px';
l.style.overflow = 'auto';
l.style.backgroundColor = '#f
0f
0f
0';
l.style.border = '1px solid gray';
l.style.fontSize = '12px';
l.style.padding = '5px';
document.body.appendChild(l);
}
return l;
}
function log(s){
Log.write(s);
}
1、将代码保存为Log.js
2、在<head></head>中插入<script type="text/javascript" src="dtree.js"></script>
3、在需要调用的地方,直接调用log(s);
调试完毕后,设置Log.showLog=false;