Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

เมธอด alert() ของ Window ใน HTML

เมธอด window.alert() ใน HTML แสดงกล่องแจ้งเตือนพร้อมข้อความที่กำหนดและปุ่ม OK ในเอกสาร HTML

ไวยากรณ์

alert("message");

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
<style>
  body { color: #000; height: 100vh; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat; text-align: center; padding: 20px; }
  .btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
</style>
</head>
<body>
<h1>เมธอด Window alert() ของ HTML</h1>
<button class='btn' onclick="add()">คลิกฉันเพื่อเปิดกล่องแจ้งเตือน</button>
<script>
  function add() { alert("สวัสดี! ฉันคือกล่องแจ้งเตือน"); }
</script>
</body>
</html>

ผลลัพธ์

คลิกปุ่ม "คลิกฉันเพื่อเปิดกล่องแจ้งเตือน" เพื่อเปิดกล่อง alert

จะแสดงกล่องโต้ตอบข้อความ "สวัสดี! ฉันคือกล่องแจ้งเตือน" พร้อมปุ่ม OK