Js控制台打印调式信息

版权声明:本文为博主落尘曦的原创文章。如转载请注明链接 【 落尘曦的博客:http://blog.csdn.net/qq_23994787 】感谢配合! https://blog.csdn.net/qq_23994787/article/details/85840702

简单用法:

console.log("日志信息");
console.info("一般信息");
console.debug("调试信息");
console.warn("警告提示");
console.error("错误提示");

格式化输出:

console.log("%d年%d月%d日", 2014, 5, 20);//日期格式输出
console.log('%c有颜色的输出信息', 'color:white; background-color:#0055CC');//格式输出

输出变量:

var who= 'you';
console.log('输出变量 We support  ', you);//读取变量

输出数组:

var arr = [1, 2, 3, 4, 5];
console.log('数组:', arr);//输出数组

IE 11 控制台

log , info , warn , error , debug , assert , time , timeEnd , group , groupCollapsed , groupEnd , trace , clear , dir , dirxml , count , countReset , cd , select , profile , profileEnd


Firebug 控制台

log , debug , info , warn , exception , assert , dir , dirxml , trace , group , groupCollapsed , groupEnd , profile , profileEnd , count , clear , time , timeEnd , timeStamp , table , error
 

Chrome 控制台

memory , _commandLineAPI , debug , error , info , log , warn , dir , dirxml , table , trace , assert , count , markTimeline , profile , profileEnd , time , timeEnd , timeStamp , timeline , timelineEnd , group , groupCollapsed , groupEnd , clear

可以看出,以上我测试的浏览器对 log , info , warn , error , debug 五个基本方法都是支持的,注意,我使用的是 IE 11,其他版本我没测试,而 Firefox 本身也是不带控制台的,需要加载Firebug 插件并且启动它,才能console,否则就是Js报错了。为了使用起来更方便,可以自己封装一下,判断一下浏览器对 console 的支持,不支持就只能使用原始的 alert 或者其他方法了。

 

猜你喜欢

转载自blog.csdn.net/qq_23994787/article/details/85840702