HTML DOM console.error() 方法

原创
ithorizon 5个月前 (10-19) 阅读数 45 #HTML

html dom console.error() 方法用于将错误消息写入控制台。此方法非常适用于测试和调试。

语法

console.error() 方法的语法如下:

console.error(console.error(message))

在这里,message是一个JavaScript字符串或对象。它是一个必需的参数值。

示例

让我们看一个关于HTML DOM console.error()方法的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>console.error() Method</h1>
<p>Click the below button to write object as error message on the console</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<button type="button" onclick="errMessage()">ERROR</button>
<script>
   function errMessage(){
      var errObj = { Message:"ERROR has been caused",Value:"NEGATIVE"};
      console.error(errObj);
   }
</script>
<p>Press F12 key to view the error message in the console </p>
</body>
</html>

输出

这将产生以下输出 −

点击ERROR按钮并查看开发者工具中的控制台选项卡 −

在上面的示例中 −

我们首先创建了一个名为ERROR的按钮,当用户点击时将执行errMessage()函数 −

<button type="button" onclick="errMessage()">ERROR</button>

errMessage()方法创建一个具有成员MessageValue及其各自值的对象。然后将此对象作为参数传递给控制台对象的error()方法。控制台的console.error()方法将对象作为错误消息打印到控制台中 −

function errMessage(){
   var errObj = { Message:"ERROR has been caused",Value:"NEGATIVE"};
   console.error(errObj);
}

以上就是HTML DOM console.error() 方法的详细内容,更多请关注IT视界其它相关文章!



热门