警告对话框经常用于确保用户可以得到某些信息!当警告框出现后,用户需要点击确定按钮才能继续进行操作
<!doctype html> <html> <head> <meta charset="utf-8"> <title>警告对话框</title> <script> function myFunction(){ alert("你好,我是一个警告框!"); } </script> </head> <body> <input type="button" value="显示警告对话框" onClick="myFunction()"> </body> </html>
确认对话框通常用于验证是否接受用户操作。当确认卡弹出时,用户可以点击 "确认" 或者 "取消" 来确定用户操作。当你点击 "确认", 确认框返回 true, 如果点击 "取消", 确认框返回 false
<!doctype html> <html> <head> <meta charset="utf-8"> <title>确认对话框</title> </head> <body> <p>点击按钮,显示确认框</p> <button onClick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction(){ var x; var r=confirm("按下按钮!"); if(r==true){ x="你按下了\"确定\"按钮!"; }else{ x="你按下了\"取消\"按钮!"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
提示对话框经常用于提示用户在进入页面前输入某个值。当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null
<!doctype html> <html> <head> <meta charset="utf-8"> <title>提示对话框</title> </head> <body> <p>点击按钮查看输入的对话框</p> <button onClick="myFunction()">点我</button> <p id="demo"></p> <script> function myFunction(){ var x; var person=prompt("请输入你的名字","Harry Potter!"); if(person!=null&&person!=""){ x="你好"+person+"今天感觉如何?"; document.getElementById("demo").innerHTML=x; } } </script> </body> </html>