1. 程式人生 > >js 三種彈窗 alert、confirm和prompt

js 三種彈窗 alert、confirm和prompt

alert:警告

經常用作程式除錯時彈出結果。在彈出後點擊"確定"按鈕之前,不能進行任何其它操作,所以可以把想要在點選“確定”按鈕之後執行的語句寫到後面就可以了,並沒有對“確定”按鈕有監聽事件

function toast(){
    alert("警告");
    console.log("點了確定");
}

alert彈出框
當點確定以後才會輸出log結果

confirm:確認框

相比alert多了一個“取消”按鈕,用法和alert相同,但是多了一個返回布林值,當點“確定”的時候會返回true,點“取消”的時候會返回false

function toast(){
    var
result = confirm("確定要退出嗎?"); if (result) { console.log("點了確定"); } else { console.log("點了取消"); } }

confirm彈出框

prompt:提示框

用於顯示可提示使用者進行輸入的對話方塊

function toast(){			
    var result = prompt("請輸入", "");
    if (result) {
        console.log("點了確定 獲取輸入的結果=" + result);
    } else
if (result === "") { console.log("點了確定但是沒有輸入內容"); } else { console.log("點了取消"); } }

prompt彈出框