ES6 对话框
ES6 对话框
JavaScript支持三种主要的对话框类型,这些对话框可以用于提醒和警报,或对任何输入进行确认。在这里我们将逐一讨论每个对话框。
Alert对话框
Alert对话框主要用于向用户发送警告消息。例如,如果一个字段需要输入内容,但用户没有提供任何输入,那么您就可以使用警告框发送警告消息。
然而,一个警报框仍然可以用于更友好的消息。警告框仅提供一个Ok按钮。
例子
<html>
<head>
<script type = "text/javascript">
function Warn() {
alert ("This is a warning message!");
document.write ("This is a warning message!");
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "Warn();" />
</form>
</body>
</html>确认对话框
确认对话框大多数用于询问用户同意与否。它显示一个对话框,有两个按钮:ok和cancel。
如果用户单击ok按钮,窗口方法confirm()将返回true。如果用户单击“cancel (取消)”按钮,则confirm()返回false。可以使用确认对话框如下。
例子
<html>
<head>
<script type = "text/javascript">
function getConfirmation(){
var retVal = confirm("Do you want to continue ?");
if( retVal == true ){
document.write ("User wants to continue!");
return true;
} else {
Document.write ("User does not want to continue!");
return false;
}
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getConfirmation();" />
</form>
</body>
</html>提示对话框
当想要弹出文本框获取用户输入时,Prompt对话框非常有用。它让网页可以与用户交互。用户需要填写字段,然后单击“确定”。
此对话框使用称为prompt()的方法显示,该方法需要两个参数: 1.您要在文本框中显示的标签和2.在文本框中显示的默认字符串。
此对话框有两个按钮:ok和cancel。如果用户单击“ok”按钮,则窗口方法prompt()将从文本框返回输入的值。如果用户单击“cancel”按钮,则窗口方法提示()返回null。
例子
<html>
<head>
<script type = "text/javascript">
function getValue(){
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
}
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getValue();" />
</form>
</body>
</html>