多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > web前端 > jscript > 為調試JavaScript添加輸出窗口

為調試JavaScript添加輸出窗口

來源:程序員人生   發布時間:2013-11-16 22:47:26 閱讀次數:2799次

調試JavaScript是一件很麻煩的事,盡管有很多很好用的調試工具,但有時候想要跟蹤值的變化,但即不想中斷腳本執行,也不想用alert顯示值信息,這種情況下,一般的做法是在頁面上添加一個DIV或者其它元素,然后再往里面添加調試信息。雖然不是很復雜的實現,但每次都要這樣就會很麻煩,所以我寫了一小段腳本,用來自動生成這個輸出窗口。

window.Babu = {};

Babu.Debugging = {};

Babu.Debugging.writeLine = function(format, arg1, arg2) {

var console = Babu.Debugging._getConsole();

if (console.get_visible()) {
var msg = format;

if (typeof msg !== "undefined" && msg !== null) {
var index;
if (typeof msg === "string") {
var array = format.match(/{(d+)}/g);
if (array) {
for (var i = 0; i < array.length; i++) {
index = array[i];
index = parseInt(index.substr(1, index.length - 2)) + 1;
msg = msg.replace(array[i], arguments[index]);
}
}
}
}

var span = document.createElement("SPAN");
span.appendChild(document.createTextNode(msg));
console._output.appendChild(span);
console._output.appendChild(document.createElement("BR"));
span.scrollIntoView();

return span;
}
}

Babu.Debugging._getConsole = function() {
var console = Babu.Debugging._console;

if (!console) {
var div = document.createElement("DIV");
div.style.position = "fixed";
div.style.right = "3px";
div.style.bottom = "3px";
div.style.width = "350px";
div.style.height = "180px";
div.style.backgroundColor = "white";
div.style.color = "black";
div.style.border = "solid 2px #afafaf";
div.style.fontSize = "12px";

document.body.appendChild(div);

Babu.Debugging._console = console = div;

div = document.createElement("DIV");
div.style.backgroundColor = "#e0e0e0";
div.style.position = "absolute";
div.style.left = "0px";
div.style.right = "0px";
div.style.top = "0px";
div.style.height = "16px";
div.style.padding = "2px 2px";
div.style.margin = "0px";
console.appendChild(div);
console._toolbar = div;

div = document.createElement("DIV");
div.style.overflow = "auto";
div.style.whiteSpace = "nowrap";
div.style.position = "absolute";
div.style.left = "0px";
div.style.right = "0px";
div.style.top = "20px";
div.style.bottom = "0px";
div.style.height = "auto";
console.appendChild(div);
console._output = div;

var btn;

btn = document.createElement("SPAN");
btn.innerHTML = "收縮";
btn.style.margin = "0px 3px";
btn.style.cursor = "pointer";
console._toolbar.appendChild(btn);
btn.onclick = function() { if (console.get_collapsed()) console.expand(); else console.collapse(); }

btn = document.createElement("SPAN");
btn.innerHTML = "清除";
btn.style.margin = "0px 3px";
btn.style.cursor = "pointer";
console._toolbar.appendChild(btn);
btn.onclick = Babu.Debugging.clearConsole;

btn = document.createElement("SPAN");
btn.innerHTML = "關閉";
btn.style.cursor = "pointer";
btn.style.margin = "0px 3px";
console._toolbar.appendChild(btn);
btn.onclick = function() { Babu.Debugging.hideConsole(); }

console.get_visible = function() { return this.style.display !== "none" };
console.get_collapsed = function() { return !(!this._collapseState) };

console.collapse = function() {
if (!this.get_collapsed()) {
this._output.style.display = "none";
this._toolbar.childNodes[1].style.display = "none";
this._toolbar.childNodes[2].style.display = "none";
this._toolbar.childNodes[0].innerHTML = "展開";
this._collapseState = { width: this.style.width, height: this.style.height }
this.style.width = "30px";
this.style.height = "16px";
}
}

console.expand = function() {
if (this.get_collapsed()) {
this._output.style.display = "";
this._toolbar.childNodes[1].style.display = "";
this._toolbar.childNodes[2].style.display = "";
this._toolbar.childNodes[0].innerHTML = "收縮";
this.style.width = this._collapseState.width;
this.style.height = this._collapseState.height;
this._collapseState = null;

}
}
}

return console;
}

Babu.Debugging.showConsole = function() {
Babu.Debugging._getConsole().style.display = "";
}

Babu.Debugging.hideConsole = function() {
var console = Babu.Debugging._console;

if (console) {
console.style.display = "none";
}
}

Babu.Debugging.clearConsole = function() {
var console = Babu.Debugging._console;
if (console) console._output.innerHTML = "";
}

調用方法很簡單:

Babu.Debugging.writeLine("調試信息");

Babu.Debugging.writeLine("帶參數的調試信息:參數1={0},參數2={1}", arg1, arg2);

調用之后,會自動在窗口的右下角出現一個固定位置的窗口,并顯示相應的內容。效果圖請看下面:

轉自:http://www.cnblogs.com/effun/

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美一区二区三区在线观看免费 | 91视频啪啪 | 精品福利国产 | 黑人双渗透 | 欧美综合在线视频 | 中国美女隐私无遮挡免费视频 | 亚洲欧美色一区二区三区 | 亚洲天堂v| 日本一级免费 | 亚洲aaaa级特黄毛片 | 欧美亚洲国产片在线观看 | 国产福利在线播放 | 在线视频一区二区三区 | 亚洲欧美久久婷婷爱综合一区天堂 | 亚洲精品中文字幕乱码影院 | 久久永久视频 | 亚洲精品aⅴ中文字幕乱码 亚洲精品h | 最近最新中文字幕高清免费 | 久久精品一区二区国产 | 久久精品亚洲 | 最近中文字幕视频国语中文字幕 | 国产成人一级 | 欧美午夜在线 | 国产欧美日韩综合精品二区 | 亚洲一级香蕉视频 | 精品国产成a人在线观看 | 在线观看欧洲成人免费视频 | 欧美一区二区三区精品国产 | 欧美japanese| 老黄网站在线观看免费 | 国产日本韩国不卡在线视频 | 在线免费观看一级毛片 | 国产资源中文字幕 | 国内精品免费视频精选在线观看 | 国产精品欧美一区二区三区不卡 | 午夜视频播放 | 国产v亚洲v天堂a无 国产v亚洲v天堂无码 | 日韩一级片免费看 | 成人国内精品久久久久影院 | 女人18毛片特级一级免费视频 | 日本高清免费毛片久久看 |