本文翻译自:JavaScript: How do I print a message to the error console?
How can I print a message to the error console, preferably including a variable? 如何将消息打印到错误控制台,最好包括变量?
For example, something like: 例如,类似于:
print('x=%d', x);
#1楼
参考:https://stackoom.com/question/glZ/JavaScript-如何将消息打印到错误控制台
#2楼
console.error(message); //gives you the red errormessage
console.log(message); //gives the default message
console.warn(message); //gives the warn message with the exclamation mark in front of it
console.info(message); //gives an info message with an 'i' in front of the message
You also can add CSS to your logging messages: 您还可以将CSS添加到日志消息中:
console.log('%c My message here', "background: blue; color: white; padding-left:10px;");
#3楼
安装Firebug ,然后您可以使用console.log(...)
和console.debug(...)
等(有关详细信息,请参阅文档 )。
#4楼
如果您使用Firebug并且需要支持IE,Safari或Opera, Firebug Lite会为这些浏览器添加console.log()支持。
#5楼
WebKit Web Inspector还支持Firebug的控制台API(只是Dan的答案的一小部分)。
#6楼
One good way to do this that works cross-browser is outlined in Debugging JavaScript: Throw Away Your Alerts! 调试JavaScript:抛出警报,概述了跨浏览器工作的一种好方法! . 。