Home > Web Front-end > JS Tutorial > Knowledge about JavaScript pop-up events

Knowledge about JavaScript pop-up events

jacklove
Release: 2018-05-07 10:28:40
Original
3001 people have browsed it

JavaScript pop-up events play an important role in warnings, verifications, etc. This article will give you some understanding of them.

JavaScript pop-up window

You can create three kinds of message boxes in JavaScript: warning box, confirmation box, and prompt box.

Alert box

Alert boxes are often used to ensure that users can get certain information.

When the warning box appears, the user needs to click the OK button to continue the operation.

Syntax

window.alert("sometext");
Copy after login

The window.alert() method can be used directly without the window object. The alert() method can be used directly.

Example

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
    alert("你好,我是一个警告框!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="显示警告框">
</body>
</html>
Copy after login

Confirmation box

Confirmation box is usually used to verify whether the user operation is accepted.

When the confirmation card pops up, the user can click "Confirm" or "Cancel" to confirm the user operation.

When you click "Confirm", the confirmation box returns true. If you click "Cancel", the confirmation box returns false.

Syntax

window.confirm("sometext");
Copy after login

The window.confirm() method can be used directly without the window object, using the confirm() method.

Example

var r=confirm("按下按钮");
if (r==true)
{
    x="你按下了\"确定\"按钮!";
}
else
{
    x="你按下了\"取消\"按钮!";
}
Copy after login

Prompt box

Prompt box is often used to prompt the user to enter a certain value before entering the page.

When the prompt box appears, the user needs to enter a certain value and then click the confirm or cancel button to continue the operation.

If the user clicks to confirm, the return value is the entered value. If the user clicks Cancel, the return value is null.

Syntax

window.prompt("sometext","defaultvalue");
Copy after login

The window.prompt() method can be used directly without the window object. The prompt() method can be used directly.

Example

var person=prompt("请输入你的名字","Harry Potter");
if (person!=null && person!="")
{
    x="你好 " + person + "! 今天感觉如何?";
    document.getElementById("demo").innerHTML=x;
}
Copy after login

Line break

The pop-up window uses backslash "n"(\n) to set line break.

Example

alert("Hello\nHow are you?");
Copy after login

js pop-up event The above has made some simple understandings. For more learning materials, please pay attention to the php Chinese website to view.

Related recommendations:

About the use of Node.js time loop

About the actual application method of jQuery Callback function

About how to use jQuery stop()

The above is the detailed content of Knowledge about JavaScript pop-up events. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template