Simple Actions

Javascript Simple Actions alert, prompt, confirm (Javascript简单操作提醒、提示、确认)

In this chapter of the tutorial you will get familiar with the browser functions. JavaScript has three kind of boxes: alert, prompt, confirm and console.log. Let’s talk about them in details.

JavaScript Alert: Definition and Usage

JavaScript Alert: Definition and Usage

alert() method shows an alert dialog with the optional detailed content and an OK button. (alert ()方法显示具有可选详细内容和OK按钮的警报对话框。)

An alert box usually used in case we want to make sure information comes through to the user. Thealert() method shows a box with a message and an “OK” button. (警报框通常用于确保信息传达给用户。Thealert ()方法显示一个带有消息和“OK”按钮的框。)

When an alert box appears, the user will have to click “OK” to begin. (出现警报框时,用户必须单击“确定”才能开始。)

Syntax

alert(message);

Example of the alert():

alert("Welcome to w3cdoc");

The window with the message is called a “modal” window. It means that the visitor can’t interact with the rest of the page or press other buttons until they have dealt with the window (press “OK”). (包含消息的窗口称为“模式”窗口。这意味着访问者在处理完窗口之前无法与页面的其余部分进行交互或按其他按钮(按“确定” )。)

The alert box takes the focus away from the window, and forces the browser to read the message. You shouldn’t overuse this method, because it prevents the user from accessing other parts of the page until the box is closed. (>警报框将焦点从窗口移开,并强制浏览器阅读消息。不应过度使用此方法,因为它会阻止用户访问页面的其他部分,直到框关闭。)

Alert dialog can be used for messages that do not require any reply on the part of the user, other than the acknowledgement of the message. (警报对话框可用于不需要用户回复的消息,除了消息的确认。)

JavaScript Prompt: Definition and Usage

JavaScript Prompt: Definition and Usage

The prompt() method displays a dialog with an optional message reminding the user to input some text. It is often used in case the user wants to input a value before entering a page. (Prompt ()方法显示带有可选消息的对话框,提醒用户输入一些文本。它通常用于用户希望在进入页面之前输入值。)

The function prompt accepts two arguments: title and default.

Syntax

prompt(text, defaultText)
result = prompt(title, [default]);

Prompt shows a modal window with a text message, an input field for the visitor, and the buttons OK/Cancel. (提示显示一个模式窗口,其中包含文本消息、访问者的输入字段和“确定/取消”按钮。)

Title - is the text to show the visitor, and default - an optional second parameter, the initial value for the input field. (标题-是显示访问者的文本,默认值-可选的第二个参数,输入字段的初始值。)

The visitor can type something in the prompt input field and press OK or cancel the input by pressing Cancel or hitting the Esc key. If the input was canceled, the call to prompt returns the text from the input field or null. (访问者可以在提示输入字段中键入内容,然后按“确定”或通过按“取消”或按Esc键取消输入。如果输入被取消,则对提示符的调用将返回输入字段中的文本或null。)

Example of the prompt():

let name = prompt("Please enter your name", "w3cdoc");
alert(`Welcome to ${name} !`); // Welcome to w3cdoc!

Javascript Confirm: Definition and Usage

Javascript Confirm: Definition and Usage

The confirm() method displays a modal window with an optional message and buttons OK and Cancel. The result will be true if OK is pressed, otherwise it will be false. (Confirm ()方法显示一个带有可选消息的模式窗口,并按OK和Cancel按钮。如果按OK键,结果为true ,否则为false。)

The confirm box takes the focus away from the current window, forcing the browser to read the message. You shouldn’t overuse this method, because it prevents the user from accessing other parts of the page until the box is closed. (>确认框将焦点从当前窗口移开,迫使浏览器阅读消息。不应过度使用此方法,因为它会阻止用户访问页面的其他部分,直到框关闭。)

Syntax:

let result = confirm("Do you want to save changes?");
alert(result); // true if OK is pressed

Javascript console.log

Javascript console.log

The console.log() method outputs a message to the web console. The message can be a single string, or anyone or more JavaScript objects. It’s useful to use console for testing purposes. (Console.log ()方法向Web控制台输出一条消息。消息可以是单个字符串,也可以是任意或多个JavaScript对象。使用控制台进行测试很有用。)

Syntax:

console.log(message);

Parameters: Accept a parameter that can be an object, an array, or any message.

Return value: Returns the value of the given parameter.

console.log() prints the component in an HTML-like tree (console.log ()在类似HTML的树中打印组件)

Example of the console.log():

console.log("Welcome to w3cdoc");


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部