HTML DOM console.warn() 方法


HTML DOM console.warn() 方法用於在控制檯中顯示警告訊息。在某些瀏覽器中,這些警告在控制檯日誌中有一個小小的驚歎號。console.warn() 方法不會中斷程式碼執行。開發人員可以使用 console.warn() 方法向用戶發出有關某些使用者操作的警告。

語法

以下是 console.warn() 方法的語法 −

console.warn(message)

此處,message 是必需引數,型別為字串或物件。它將在控制檯中顯示為一個警告。

示例

我們來看一個 console.warn() 方法的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>console.warn() Method</h1>
<p>Click the below button more than 4 times</p>
<button onclick="warning()" type="button">CLICK</button>
<script>
   var i=0;
   function warning(){
      i++;
      if(i>4)
      console.warn("You have clicked this button too many times");
   }
</script>
<p>Press F12 key to view the warning message in the console.</p>
</body>
</html>

輸出

將生成以下輸出 −

在連續點選 CLICK 按鈕 4 次以上並檢視控制檯標籤中的輸出時。

在上例中 −

我們建立了一個按鈕 CLICK ,使用者點選後,該按鈕將執行 warning() 方法。

<button onclick="warning()" type="button">CLICK</button>

warning() 方法會增加變數 i。如果變數 i 大於 4,則執行 console.warn(msg) 方法,該方法將 msg 引數顯示為警告 −

var i=0;
function warning(){
   i++;
   if(i>4)
   console.warn("You have clicked this button too many times");
}

更新於: 08-Aug-2019

143 閱讀次數

開啟您的 職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.