Set output color in javascript

I saw this code in other question , and i know that it works in chrome console, but this not work in javascript console, like jsbin or stackoverflow snippet.

How can i make it work in all console, with compatibility?

console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');

You simply can't. They need to implement it first.


这里是一个非常简单的没有“%”语法的实现(我只是想告诉你,你并不需要这个特性):

var styles = "background:#222;color:#bada55";
log("you could do", style(styles, "this"));
log("you could do", style(styles, "this"));
log("you could do", style(styles, "this"));
<pre id="pre"></pre>
<style>div{border-bottom:1px solid #ddd;padding:.5em}</style>
<script>!function(pre){this.log=function(){var a=document.createElement("div");a.innerHTML=Array.prototype.join.call(arguments," ");pre.appendChild(a)};this.style=function(a,b){return'<span style="'+a+'">'+b+"</span>"};}(document.getElementById("pre"));</script>
链接地址: http://www.djcxy.com/p/87386.html

上一篇: 着色来自PHP的Windows命令行输出

下一篇: 在javascript中设置输出颜色