Colors in JavaScript console

前端 未结 27 2495
说谎
说谎 2020-11-22 11:42

Can Chrome\'s built-in JavaScript console display colors?

I want errors in red, warnings in orange and console.log\'s in green. Is that possible?

27条回答
  •  独厮守ぢ
    2020-11-22 12:19

    I actually just found this by accident being curious with what would happen but you can actually use bash colouring flags to set the colour of an output in Chrome:

    console.log('\x1b[36m Hello \x1b[34m Colored \x1b[35m World!');
    console.log('\x1B[31mHello\x1B[34m World');
    console.log('\x1b[43mHighlighted');
    

    Output:

    See this link for how colour flags work: https://misc.flogisoft.com/bash/tip_colors_and_formatting

    Basically use the \x1b or \x1B in place of \e. eg. \x1b[31m and all text after that will be switched to the new colour.

    I haven't tried this in any other browser though, but thought it worth mentioning.

提交回复
热议问题