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弹出框

猜你喜欢

转载自blog.csdn.net/zyw0101/article/details/84837448
今日推荐