Colors in JavaScript console

前端 未结 27 2628
说谎
说谎 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:29

    There are a series of inbuilt functions for coloring the console log:

    //For pink background and red text
    console.error("Hello World");  
    
    //For yellow background and brown text
    console.warn("Hello World");  
    
    //For just a INFO symbol at the beginning of the text
    console.info("Hello World");  
    
    //for custom colored text
    console.log('%cHello World','color:blue');
    //here blue could be replaced by any color code
    
    //for custom colored text with custom background text
    console.log('%cHello World','background:red;color:#fff')
    

提交回复
热议问题