簡體   English   中英

創建一個顯示警報的通用函數,並接受要在警報中顯示的值作為輸入

[英]create a generic function that displays an alert, and accepts as input the value that is to be displayed in the alert

函數被調用。 JavaScript函數是旨在執行特定任務的代碼塊。

當“某物”將其調用(調用)時,將執行JavaScript函數。

function displays_Five() {
  window.alert("Five");
}
function displays_Four() {
  window.alert("Four");
}
function displays_Three() {
  window.alert("Three");
}
function displays_Two() {
  window.alert("Two");
}
function displays_One() {
  window.alert("One");
}

這是一種方法:

window.alert("Five");
window.alert("Four");
window.alert("Three");
window.alert("Two");
window.alert("One");

現在,如果您真的想創建包裝函數:

function myAlert(str) {
    window.alert(str);
}
myAlert("Five");
myAlert("Four");
myAlert("Three");
myAlert("Two");
myAlert("One");

 function myAlert(str) { window.alert(str); } myAlert("Five"); myAlert("Four"); myAlert("Three"); myAlert("Two"); myAlert("One"); 

function showAlert(contents) {
    alert(contents);
}

但是,為什么不只使用警報功能呢?

簡單為:

function displayAlert(text) {
    alert(text);
}

// Prompt from client
var input = prompt('What you want?');

// Make sure input is not empty (NULL) then display client input
if(input) displayAlert(input);

// Or display static text
displayAlert('Well Done.')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM