在JavaScript中,可以使用alert()函數來彈出一個警告框,用于向用戶顯示一條消息。alert()函數只有一個參數,即要顯示的消息。
以下是一些使用alert()函數的示例:
alert("Hello World!");
var name = "John";
alert("Hello " + name);
var x = 10;
var y = 5;
alert("The sum of " + x + " and " + y + " is " + (x + y));
var age = prompt("Please enter your age:");
if (age < 18) {
alert("You are not old enough to access this site.");
} else {
alert("Welcome to our website!");
}
var result = confirm("Are you sure you want to delete this item?");
if (result) {
alert("Item deleted successfully.");
} else {
alert("Deletion canceled.");
}
這些只是alert()函數的一些基本用法示例,實際上可以根據需要進行更復雜的操作和顯示。