js中三种弹框及其特点

1、alert()

作用:

alert() 方法用于显示带有一条指定消息和一个 OK 按钮的警告框。

语法:

alert(message)

参数:message:要在 window 上弹出的对话框中显示的纯文本(而非 HTML 文本)

返回值:

实例:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function display_alert()
 5   {
 6   alert("I am an alert box!!")
 7   }
 8 </script>
 9 </head>
10 <body>
11 
12 <input type="button" onclick="display_alert()"
13 value="Display alert box" />
14 
15 </body>
16 </html>

 

2、confirm()

作用:

confirm() 方法用于显示一个带有指定消息和 OK 及取消按钮的对话框。

语法:

confirm(message)

参数:message:要在 window 上弹出的对话框中显示的纯文本(而非 HTML 文本)

返回值:

如果用户点击确定按钮,则 confirm() 返回 true。如果点击取消按钮,则 confirm() 返回 false。

实例:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function disp_confirm()
 5   {
 6   var r=confirm("Press a button")
 7   if (r==true)
 8     {
 9     document.write("You pressed OK!")
10     }
11   else
12     {
13     document.write("You pressed Cancel!")
14     }
15   }
16 </script>
17 </head>
18 <body>
19 
20 <input type="button" onclick="disp_confirm()"
21 value="Display a confirm box" />
22 
23 </body>
24 </html>

 

3、prompt(“”,“”)

作用:

prompt() 方法用于显示可提示用户进行输入的对话框。

语法:

prompt(text,defaultText)

参数:text:可选。要在对话框中显示的纯文本(而不是 HTML 格式的文本)。

  defaultText:可选。默认的输入文本。

返回值:

如果用户单击提示框的取消按钮,则返回 null。如果用户单击确认按钮,则返回输入字段当前显示的文本。

在用户点击确定按钮或取消按钮把对话框关闭之前,它将阻止用户对浏览器的所有输入。在调用 prompt() 时,将暂停对 JavaScript 代码的执行,在用户作出响应之前,不会执行下一条语句。

实例:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4 function disp_prompt()
 5   {
 6   var name=prompt("Please enter your name","")
 7   if (name!=null && name!="")
 8     {
 9     document.write("Hello " + name + "!")
10     }
11   }
12 </script>
13 </head>
14 <body>
15 
16 <input type="button" onclick="disp_prompt()"
17 value="Display a prompt box" />
18 
19 </body>
20 </html>

 

posted @ 2021-02-10 17:23  飞渝  阅读(573)  评论(0)    收藏  举报