49、console.log()花式玩法

效果图:


image.png

附上代码:

 console.log(`%c                                                                            
                               %c FBI WARNING %c                                
%c        Federal Law provides severe civil and criminal penalties for        
        the unauthorized reproduction,distribution, or exhibition of        
         copyrighted motion pictures (Title 17, United States Code,         
        Sections 501 and 508). The Federal Bureau of Investigation          
         investigates allegations of criminal copyright infringement        
                 (Title 17, United States Code, Section 506).               `,
    'background: #000; font-size: 18px; font-family: monospace',
    'background: #f33; font-size: 18px; font-family: monospace; color: #eee; text-shadow:0 0 1px #fff',
    'background: #000; font-size: 18px; font-family: monospace',
    'background: #000; font-size: 18px; font-family: monospace; color: #ddd; text-shadow:0 0 2px #fff'
    )

为什么会这样呢?想必你还记得其他语言中的print()。占位符是print()的专属吗?不,他们在console.log()中同样适用:

%s:字符串
%d:整数
%i:整数
%f:浮点数
%o:obj对象(DOM)
%O:obj对象
%c:CSS样式

console.log()可以通过以上这些特有的占位符进行信息的加工输出。是的,你可能已经明白,上面代码的玄机就在四个%c,
第一个创建神秘而性感的纯黑背景;
第二个给“FBI WARNING”加上红色的背景;
第三个恢复纯黑的性感;
第四个配上白色的文字,如此,大事已成。

明白了以上原理,诸君就可以自由发挥,展示你们强大的css实力了,甚至还可以输出gif背景图

可以清空控制台console.clear()

console.clear()

可以打印表格console.table()

var languages = [
  { name: "JavaScript", fileExtension: ".js" },
  { name: "TypeScript", fileExtension: ".ts" },
  { name: "CoffeeScript", fileExtension: ".coffee" }
];

console.table(languages);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容