js:三种弹出框(alter警告,confirm确认,prompt提示)

发布时间 2023-03-29 19:57:37作者: sunny123456

js:三种弹出框(alter警告,confirm确认,prompt提示)
https://blog.csdn.net/qq_52824207/article/details/124081188

三种弹出框都属于window对象,完整写作window.alter,三种弹出框都可省略window.

alter弹出警告框,只有一个确定按钮

alert("这是一个警告框");

confirm弹出确认框,有两个按钮:确定和取消,分别返回true和false

confirm("这是一个确认框,确定返回true,取消返回false");

prompt弹出提示框,确定,取消,输入框,确定返回输入框的值(不输入为空""),取消返回Null

prompt("这是一个提示框,确定返回输入值,取消返回Null","输入框内默认文本");

完整测试代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>弹出框测试</title>
  5. </head>
  6. <body style="text-align: center;">
  7. <h1>弹出框测试</h1>
  8. <button onclick="test_1()">警告框</button>
  9. <button onclick="test_2()">确认框</button>
  10. <button onclick="test_3()">提示框</button>
  11. <p id="demo"></p>
  12. </body>
  13. <script>
  14. function test_1(){
  15. alert(("这是一个警告框"));
  16. }
  17. function test_2(){
  18. test2 = confirm("这是一个确认框,确定返回true,取消返回false");
  19. document.getElementById("demo").innerHTML = test2;
  20. }
  21. function test_3(){
  22. var test3 = prompt("这是一个提示框,确定返回输入值,取消返回Null","输入框内默认文本");
  23. document.getElementById("demo").innerHTML = test3;
  24. }
  25. </script>
  26. </html>