console.log()能打出不同颜色的字吗?需要怎么填参数?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了console.log()能打出不同颜色的字吗?需要怎么填参数?相关的知识,希望对你有一定的参考价值。

参考技术A

可以打出不同颜色的字,在不同游览器中可以通过以下参数来实现。

chrome浏览器:console.log('%c this is color! ', 'background: #222; color: #bada55‘);

IE游览器:console.log('%c 测试 ', 'color: #e5bd9c‘);

javascript 颜色console.log()输出

Below you can find colors reference of text to command when running node.js application:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow
Note %s is where in the string (the second argument) gets injected. 
\x1b[0m resets the terminal color so it doesn't continue
to be the chosen color anymore after this point.

Colors reference

Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"


EDIT:

For example, \x1b[31m is an escape sequence that will be intercepted by your terminal 
and instructs it to switch to the red color. 
In fact, \x1b is the code for the non-printable control character escape. 
Escape sequences dealing only with colors and styles are also known as ANSI escape code and are standardized, 
so therefore they (should) work on any platform.

以上是关于console.log()能打出不同颜色的字吗?需要怎么填参数?的主要内容,如果未能解决你的问题,请参考以下文章

python怎么用turtle打出不同颜色的字?

VSCode特定的字颜色

chalk插件 使终端输出的字带颜色

常说的字面值

烟花放出来可以显示字?

安卓手机上怎么打出好看的字体?