覆盖警报并在Javascript中确认 [英] Override alert and confirm in Javascript

查看:78
本文介绍了覆盖警报并在Javascript中确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在javascript中覆盖 alert() confirm()

Is there a way to override alert("") and confirm("") in javascript?

我使用jQuery,所以如果有办法在这个框架中做到这一点,我会很感激。

I use jQuery, so if there is a way to do it in this framework, I'd appreciate it.

我想做的事情如下:

override alert(msg){
    //Show custom stuff here
}

override confirm(msg){
    //Show custom stuff here.
    return watever;
}


推荐答案

是和否。你可以简单地替换它们:

Yes and no. You can simply replace them:

window.alert = function(msg){
    // stuff
}

但是你不会得到提醒和确认给你的阻止功能,即那里将无法使此代码正常工作:

but you're not going to get the blocking functionality that alert and confirm give you, i.e. there will be NO way to get this code to work:

if(confirm('Do Something?')){
   // do stuff
}

你通常最好不要做其他功能与你试图取代这些相似的东西。

You're generally better off making other functions to do similar things than you are trying to replace these.

这篇关于覆盖警报并在Javascript中确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆