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

时间:2021-04-13 00:07:56

标签: javascript html css warnings console.log

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

The message has a pale, yellow background with brown text and is prefaced with a yellow warning triangle.

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

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

但使用 console.warning(message) 返回 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>

但它不起作用。我该怎么做?

1 个答案:

答案 0 :(得分:3)

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

示例

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

参考

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