Home  >  Article  >  Web Front-end  >  What are the JavaScript pop-up boxes?

What are the JavaScript pop-up boxes?

青灯夜游
青灯夜游Original
2021-04-07 18:09:464964browse

JavaScript pop-up boxes include: 1. Alert box, which has only one button "OK" and no return value. It is often used to ensure that users can get certain information; 2. Confirmation box, which has two buttons: "OK" and "Cancel" A button, which returns a true or false value, is often used to allow users to verify or accept certain information; 3. Prompt box, which returns the entered message.

What are the JavaScript pop-up boxes?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The three dialog boxes of JavaScript are obtained by calling the three methods alert(), confirm() and prompt() of the window object. You can use these dialog boxes to complete js Input and output, 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 journey `(*∩_∩*)′...

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 dialog box. The information to be displayed is placed in brackets, and the dialog box contains a "Confirm" button that the user can simply click to close the dialog box after reading the displayed information. 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 the sentence "Shanglian: Ancient trees are dead under the rocks", As shown below:

What are the JavaScript pop-up boxes?

Then, click the "Confirm" button and then the second dialog box will be displayed and "The girl beside the white water spring is wonderful!", the effect is as follows;

What are the JavaScript pop-up boxes?

A dialog box pops up on the page and displays the sentence "Shanghai Lien: The ancient trees are dead under the rocks." After clicking the "Confirm" button, the second dialog box is displayed and displayed. "The girl beside the white water spring is wonderful!" Let's analyze this small example:

a. Call the alert() method twice in the <script> script block; </script>

b. In each A piece of text information is added to 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. Click the "OK" button again. 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 and brackets displayed above. Information within,

[Recommended learning: javascript advanced tutorial]

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 a confirmation 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:

What are the JavaScript pop-up boxes?

Analyze this small example:

a. Add the confirm() method in the <script> script block. </script>

b. Add a piece of text information in the confirm() brackets. The running effect is as shown in the figure above. If the user When the "Confirm" button is clicked, the confirm() method will return true. If the user clicks the "Cancel" button, the confirm() method will return false. 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>

Let’s analyze this small example:

a. A variable con is declared in the <script> script block. </script>

b. The sentence con=confirm() assigns the Boolean value returned by the confirm() method to con.

c. Use the value of con through the if statement to execute different statements respectively; the execution effect is as follows:

What are the JavaScript pop-up boxes?

If you click the confirmation box on the page After clicking the "OK" button on the screen, a page as shown below appears:

What are the JavaScript pop-up boxes?

如果单击“取消”按钮,则出现如下图所示的页面:

What are the JavaScript pop-up boxes?

第三种: 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>

运行上面的程序,效果如下所示:

What are the JavaScript pop-up boxes?

点击确定,会有这么惊喜nie:

What are the JavaScript pop-up boxes?

我们再点击确定按钮:

What are the JavaScript pop-up boxes?

再点击确定按钮:

What are the JavaScript pop-up boxes?

分析一下这个小例子

a、在<script>脚本块中添加了两个prompt()方法。</script>

b、在第一个prompt()括号内添加了一段文本信息。

c、name=prompt()一句是将用户在文本框中输入的信息赋给变量name。

alert()、confirm()、prompt()的区别和联系:

警告框alert()

alert是警告框,只有一个按钮“确定”无返回值,警告框经常用于确保用户可以得到某些信息。当警告框出现后,用户需要点击确定按钮才能继续进行操作。语法:alert("文本")。

确认框confirm()

confirm是确认框,两个按钮,确定或者取消,返回true或false。确认框用于使用户可以验证或者接受某些信息。当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作。如果用户点击确认,那么返回值为 true。如果用户点击取消,那么返回值为 false。语法:confirm("文本")

提示框prompt()

prompt是提示框,返回输入的消息,或者其默认值提示框经常用于提示用户在进入页面前输入某个值。当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。语法:prompt("文本","默认值")

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What are the JavaScript pop-up boxes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn