在控制台中显示黄色警告消息

Display yellow warning message in console

我想在 Chrome 控制台中显示一条警告消息,例如此屏幕截图中突出显示的项目:

console.log(message) 显示正常的白色消息。

console.error(message) 创建一条错误消息。

但使用 console.warning(message) returns Uncaught TypeError: console.warning is not a function.

那么有什么方法可以为 JavaScript 显示控制台警告吗?

应该是这样的:

(function() {
  var newbgcolor = document.getElementById('mycolor').value;
  document.getElementById('output').style.backgroundColor = newbgcolor;
});

function update() {
  var mycolorvalue = document.getElementById('mycolor').value;
  if (mycolorvalue != "#000000") {
    document.getElementById('output').style.backgroundColor = mycolorvalue;
  } else {
    console.warning("Text will be hard to read!"); // <-- error happens
  }
}
#output {
  background-color: #00ffff;
}
<!DOCTYPE html public "-//W3C//HTML 4.01 Transitional//EN">
<html>

<head>
  <meta name="viewport" content="width=device-width,initial-scale=1.0" />
</head>

<body>
  <p>Background color:</p>
  <input type="color" id="mycolor" value="#00ffff" oninput="update()" />
  <p id="output">You will see the change here.</p>
</body>

</html>

但是没用。我该怎么做?

编辑:我可以使用自定义 CSS!

console.log(
  "%c Foo",
  "display:block;width:100%;background-color: #332B00;color:#F2AB26;"
);

但它看起来不对:

尝试使用 console.warn 方法代替 console.warning

例子

console.warn("A sample warning message!");

参考

这是包含可用控制台方法的完整列表的资源: https://developer.mozilla.org/en-US/docs/Web/API/console#methods