HTML DOM console.groupEnd() 方法


HTML DOM console.groupEnd() 方法用於指示訊息組的結束。它退出控制檯中的當前訊息組。

語法

以下列示了 console.groupEnd() 方法的語法 −

console.groupEnd()

示例

讓我們來看一下 HTML DOM console.groupEnd() 方法的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>console.groupEnd() Method</h1>
<p>Press F12 key to view the message in the console view.</p>
<button type="button" onclick="groupMessage()">GROUP</button>
<button type="button" onclick="EndGroup()">END GROUP</button>
<script>
   function groupMessage(){
      console.group();
      console.log("This message will be inside a group!");
      console.log("This message will also be inside a group!");
   }
   function EndGroup(){
      console.groupEnd();
      console.log("We are now outside the group");
      console.log("This message will be outside the group!");
   }
</script>
</body>
</html>

輸出

它會生成以下輸出 −

在點選 GROUP 按鈕並在開發者選項中檢視控制檯檢視時 −

在點選 END GROUP 按鈕並在開發者選項中檢視控制檯檢視時 −

在上述示例中 −

我們建立了兩個按鈕 GROUP 和 END GROUP,在使用者點選時它們將執行 groupMessage() 和 EndGroup() 方法 −

<button type="button" onclick="groupMessage()">GROUP</button>
<button type="button" onclick="EndGroup()">END GROUP</button>

groupMessage() 方法內部有 console.group() 方法,說明在該點之後寫入的所有訊息都將顯示在訊息組中 −

function groupMessage(){
   console.group();
   console.log("This message will be inside a group!");
   console.log("This message will also be inside a group!");
}

EndGroup() 方法內部有 console.groupEnd() 方法,說明在該點之後寫入的所有訊息都將顯示在訊息組外部。如果之前不存在訊息組,則它不會引發錯誤 −

function EndGroup(){
   console.groupEnd();
   console.log("We are now outside the group");
   console.log("This message will be outside the group!");
}

更新於: 08-Aug-2019

61 次瀏覽

開啟您的 職業 生涯

透過完成課程獲得認證

開始
廣告