Home > Article > Web Front-end > Three ways to pop up a dialog box in JavaScript_javascript tips
Friends who have studied js will find that we have used the alert() method, prompt() method, and prompt() method in some instances. They all pop up a dialog box on the screen and display brackets on it. Content inside, using this method makes the interactivity of the page more exciting. In fact, we often use this type of dialog box when browsing web pages. Dialog boxes are often used when users have two-way communication with applications. .
The three dialog boxes of JavaScript are obtained by calling the three methods alert(), confirm() and prompt() of the window object. These dialog boxes can be used to complete the input and output of js. , implement js code that can interact with users.
Today, the editor will briefly introduce the three pop-up dialog boxes in js. The editor will first explain these methods in detail separately, and then compare these methods. Okay, let’s start our js Let’s go on a journey`(*∩_∩*)′......
The first: alert() method
The alert() method is the easiest to use among these three dialog boxes. It can be used to simply and clearly display the text information in the alert() brackets in the dialog box. We call it an alert. A dialog box in which the information to be displayed is placed in parentheses. The dialog box contains a "Confirm" button. After the user has finished reading the displayed information, he only needs to click the button to close the dialog box. Let’s look at an example of using the alert() method. The code is as follows:
<html> <head> <title>编写html页面</title> <script language="javascript"> //JavaScript脚本标注 alert("上联:山石岩下古木枯");//在页面上弹出上联 alert("下联:白水泉边少女妙");//在页面上弹出下联 </script> </head> </html>
Execute the above small example, a dialog box will pop up on the page and display the sentence "Shanglian: The ancient trees are dead under the rocks and rocks", as shown below:
Then, after clicking the "Confirm" button, the second dialog box will be displayed and "The girl beside the white water spring is wonderful!", the effect is as follows;
A dialog box pops up on the page and displays the sentence "First couplet: Ancient trees withered under the rocks." After clicking the "Confirm" button, a second dialog box displays and displays "The girl beside the white water spring is wonderful!" Let's analyze it. Here’s a small example:
a. Call the alert() method twice in the 3f1c4e4b6b16bbbd69b2ee476dc4f83a script block;
b. Add a piece of text information in each alert() bracket. When running, the page shown below will appear. When you click the "OK" button on the page with the mouse, the second page will appear, and then Click the "OK" button to close the dialog box on the page. Note: The two dialog boxes are displayed separately, rather than one covering the other. This is because js actually executes the first alert() and waits until the user clicks the "Confirm" button before executing the second alert(). .
alert() is a method of the js window object. When called, it can be written as window.alert() or alert(). Its function is to generate a dialog box with a confirmation button, which displays the information in brackets. ,
Second: confirm() method
The confirm() method is very similar to the alert() method. The difference is that in addition to a "confirm" button, this type of dialog box also has a "cancel" button. This type of dialog box is called confirmation. In the dialog box, you do not need to write window when calling the confirm() method of the window object and the prompt() method introduced later. Let’s look at a small example about confirm(). The code is as follows:
<html> <head> <title>编写html页面</title> <script language="javascript"> //js脚本标注 confirm("上联:一但重泥拦子路;下联:两岸夫子笑颜回"); //在页面上弹出确认对话框 </script> </head> </html>
The display effect is as follows:
Analyze this small example:
a. Add the confirm() method in the 3f1c4e4b6b16bbbd69b2ee476dc4f83a script block,
b. A piece of text information is added within the confirm() brackets. The running effect is as shown in the picture above. If the user clicks the "Confirm" button, the confirm() method will return true. If the user clicks the "Cancel" button , the confirm() method will return false, and no matter which button the user selects, the dialog box will be closed and the JavaScript code will continue to be executed. Clicking the "Confirm" or "Cancel" button both closes the dialog box. There seems to be no difference. In fact, whether clicking the "Confirm" or "Cancel" button will return a Boolean value, so that there can be some js behind the scenes. Code to play the role of the button. Please look at the example below to experience the beauty of using confirm() to return a Boolean value. The code is as follows:
<html> <head> <title>编写html页面</title> <script language="javascript"> //js脚本标注 var con; con=confirm("你喜欢玫瑰花么?"); //在页面上弹出对话框 if(con==true)alert("非常喜欢!"); else alert("不喜欢!"); </script> </head> </html>
我们来分析一下这个小例子:
a、在3f1c4e4b6b16bbbd69b2ee476dc4f83a脚本块中声明了一个变量con。
b、con=confirm()一句将confirm()方法返回的布尔值赋给con。
c、通过if语句来使用con的值,分别执行不同的语句;执行的效果如下:
如果单击页面的确认框上的“确定”按钮后,出现如下图所示的页面:
如果单击“取消”按钮,则出现如下图所示的页面:
第三种: prompt()方法
alert()方法和confirm()方法的使用十分类似,都是仅仅显示已有的信息,但用户不能输入自己的信息,但是prompt()可以做到这点,她不但可以显示信息,而且还提供了一个文本框要求用户使用键盘输入自己的信息,同时她还包含“确认”或“取消”两个按钮,如果用户“确认”按钮,则prompt()方法返回用户在文本框中输入的内容(是字符串类型)或者初始值(如果用户没有输入信息);如果用户单击“取消”按钮,则prompt()方法返回null,我们称这种对话框为提示框,在这三种对话框中,她的交互性最好。
看下面一个小例子:在页面上两次弹出提示对话框,使用户能输入有关信息,代码如下:
<html> <head> <title>编写html页面</title> <script language="javascript"> //js脚本标注 var name,age; name=prompt("请问你叫什么名字?"); /*在页面上弹出提示对话框, 将用户输入的结果赋给变量name*/ alert(name); //输出用户输入的信息 age=prompt("你今年多大了?","请在这里输入年龄"); /*在页面上再一次弹出提示对话框, 讲用户输入的信息赋给变量age*/ alert(age)//输出用户输入的信息 </script> </head> </html>
运行上面的程序,效果如下所示:
点击确定,会有这么惊喜nie:
我们再点击确定按钮:
再点击确定按钮:
分析一下这个小例子
a、在3f1c4e4b6b16bbbd69b2ee476dc4f83a脚本块中添加了两个prompt()方法。
b、在第一个prompt()括号内添加了一段文本信息。
c、name=prompt()一句是将用户在文本框中输入的信息赋给变量name。
alert()、confirm()、prompt()的区别和联系:
警告框alert()
alert是警告框,只有一个按钮“确定”无返回值,警告框经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。语法:alert("文本")。
确认框confirm()
confirm是确认框,两个按钮,确定或者取消,返回true或false。确认框用于使用户可以验证或者接受某些信息。当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。语法:confirm("文本")
提示框prompt()
prompt是提示框,返回输入的消息,或者其默认值提示框经常用于提示用户在进入页面前输入某个值。当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。语法:prompt("文本","默认值")
本文主要介绍了javascript中的三种弹出对话框,分别是alert()方法,confirm()方法,prompt()方法,小编先对这几个方法进行了详细的单独介绍,紧接着,将这几个方法进行对比,除这三个弹出对话框之外,我们还可以使用document.write()直接将消息显示在页面上,BS学习,未完待续......同时感谢大家一直以来对脚本之家网站的支持!