且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

js调试工具Console命令详解

更新时间:2022-08-17 18:45:06

可能大家对console.log会有一定的了解,心里难免会想调试的时候用alert不就行了,干嘛还要用console.log这么一长串的字符串来替代alert输出信息呢,下面我就介绍一些调试的入门技巧,让你爱上console.log


一、显示信息的命令

最常用的就是console.log了。

效果:
http://hovertree.com/texiao/js/34/1.htm

查看效果的方法:如果是Chrome浏览器,请打开“开发者工具”,按快捷键“Ctrl+Shift+I”,或者右键点击页面,选择“检查”菜单。然后在“Console”面板可以查看输出结果。如图所示:
js调试工具Console命令详解

如果是火狐浏览器的话,按组合键“Ctrl+Shift+K”可以打开“网页控制台”。如图:
js调试工具Console命令详解

示例的代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>常用console命令之显示信息_何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>常用console命令之显示信息,请查看浏览器的console面板。

<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<script type="text/javascript">
console.log('hello hovertree');
console.info('信息 何问起');
console.error('错误');
console.warn('警告');
</script>
</body>
</html>


二:占位符

console上述的集中度支持printf的占位符格式,支持的占位符有:字符(%s)、整数(%d或%i)、浮点数(%f)和对象(%o)

示例代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>console命令之占位符_何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>console命令之占位符 <a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a></div>
<script type="text/javascript">
console.log("%d年%d月%d日",2016,11,11);
</script>
</body>
</html>


查看效果:
http://hovertree.com/texiao/js/34/2.htm
效果图:
js调试工具Console命令详解

三、信息分组

示例代码:
<!DOCTYPE html>
<html>
<head><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>常用console命令_何问起</title><base target="_blank" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>常用console命令之信息分组
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<script type="text/javascript">
console.group("第一组信息");
console.log("第一组第一条:何问起(http://hovertree.com)");
console.log("第一组第二条:柯乐义(http://keleyi.com)");
console.groupEnd();

console.group("第二组信息");
console.log("第二组第一条:HoverClock 一个jQuery时钟插件");
console.log("第二组第二条:欢迎使用");
console.groupEnd();
</script>
</body>
</html>

查看效果:
http://hovertree.com/texiao/js/34/

效果图:
js调试工具Console命令详解

四、查看对象的信息

console.dir()可以显示一个对象所有的属性和方法。
示例代码:
<!DOCTYPE html>
<html>
<head><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>用console.dir方法查看对象信息_何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>
用console.dir方法查看对象信息
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<script type="text/javascript">
var info = {
blog:"http://hovertree.com",
时钟插件:"HoverClock",
message:"欢迎使用!"
};
console.dir(info);
</script>
</body>
</html>

查看效果:
http://hovertree.com/texiao/js/34/4.htm

效果图:
js调试工具Console命令详解

五、显示某个节点的内容

console.dirxml()用来显示网页的某个节点(node)所包含的html/xml代码。

示例代码:
<!DOCTYPE html>
<html>
<head><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>常用console命令_何问起</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><base target="_blank" />
<style>a{color:blue;}</style>
</head>
<body>
<div>
console.dirxml()用来显示网页的某个节点(node)所包含的html/xml代码
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<div id="info">
<h3>我的博客:hovertree.com</h3>
<p>HoverTreeImg插件,欢迎使用</p>
</div>
<script type="text/javascript">
var info = document.getElementById('info');
console.dirxml(info);
</script>
</body>
</html>


查看效果:
http://hovertree.com/texiao/js/34/5.htm

效果图:
js调试工具Console命令详解

六、判断变量是否是真

console.assert()用来判断一个表达式或变量是否为真。如果结果为否,则在控制台输出一条相应信息,并且抛出一个异常。

示例代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>console.assert使用_何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>
console.assert方法的使用
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a>
</div>
<script type="text/javascript">
var result = 1;
console.assert( result );
var year = 2014;
console.assert(year == 2018 );
</script>
</body>
</html>


效果:
http://hovertree.com/texiao/js/34/6.htm

效果图:
js调试工具Console命令详解

七、追踪函数的调用轨迹。

console.trace()用来追踪函数的调用轨迹。
示例代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>追踪函数的调用轨迹_何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:blue;}</style>
</head>
<body>
<div>javascript中console.trace()方法示例
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a></div>
<script type="text/javascript">
/*函数是如何被调用的,在其中加入console.trace()方法就可以了 -- 何问起*/
function add(a,b){
console.trace();
return a+b;
}
var x = add3(1,1);
function add3(a,b){return add2(a,b);}
function add2(a,b){return add1(a,b);}
function add1(a,b){return add(a,b);}
</script>
</body>
</html>


查看效果:
http://hovertree.com/texiao/js/34/7.htm

效果图:
js调试工具Console命令详解

八、计时功能

console.time()和console.timeEnd(),用来显示代码的运行时间。

示例代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>显示代码的运行时间_何问起</title><base target="_blank" />
<meta charset="utf-8" />
</head>
<body>
<div>console.time()和console.timeEnd(),用来显示代码的运行时间。
<a href="http://hovertree.com/h/bjaf/gk6698g3.htm">说明</a>
<a href="http://hovertree.com">首页</a></div>
<script type="text/javascript">
console.time("控制台计时器一");
for(var i=0;i<10000;i++){
for(var j=0;j<1000;j++){}
}
console.timeEnd("控制台计时器一");
</script>
</body>
</html>


查看效果:
http://hovertree.com/texiao/js/34/8.htm

效果图:

js调试工具Console命令详解