2014-03-03 76 views
60

我今天在Facebook的Chrome中通过控制台“摇摆”。
令人惊讶的是,我在控制台中收到了这条消息。

现在我的问题是:
这怎么可能?
我知道控制台有一些'exploit'方法,但是如何在控制台中进行这种字体格式化呢? (并且它CONSOLE.LOG?)如何创建格式化的javascript控制台日志消息

回答

19

试试这个:

console.log("%cThis will be formatted with blue text", "color: blue"); 

引用的文档,

您使用%C格式说明应用自定义CSS规则,任何 字符串你用console.log()或相关的方法写入控制台。

来源:

console.log("%cExtra Large Yellow Text with Red Background", "background: red; color: yellow; font-size: x-large"); 

注意%c在第二位的第一个参数文字和样式规格前面: https://developers.google.com/web/tools/chrome-devtools/console/console-write#styling_console_output_with_css

6

从谷歌的网站:site

console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large"); 
7

您也可以格式化字符串。

var red = 'color:red'; 
var blue = 'color:blue'; 
console.log('%cHello %cWorld %cfoo %cbar', red, blue, red, blue); 

enter image description here

相关问题