css代码:

 /*custom_alert and custom_confirm*/
.custom_popupFilterBg {width: 100%; height: 100%; background-color: #000; filter: alpha(opacity=60); opacity: 0.6; position: fixed; z-index:; }
.custom_popupContent{position: fixed; left: 50%; top: 40%; z-index:; filter: alpha(opacity=0); opacity:;background-color: #585858; padding: 30px 30px; border: 4px solid #ccc; border-radius: 10px; min-width: 180px; max-width:600px;text-align: center; }
.custom_popupContent .custom_popupTipsText{font-size:20px;}
.custom_popupContent .custom_popupTipsText.alignLeft{text-align: left;}
.custom_popupContent .custom_popupBtnsContainer{text-align: center;margin-top:30px;}
.custom_popupContent .custom_popupBtnsContainer input[type='button']{border: 1px solid #0F620A; color: #fff; cursor: pointer; height: 28px; line-height: 28px; font-size: 12px;min-width: 68px; border-radius: 4px; }
.custom_popupContent .custom_popupBtnsContainer input[type='button']:first-child{background-color: #1DA514;margin-right:10px;margin-right: 1rem;}
.custom_popupContent .custom_popupBtnsContainer input[value='Cancel']{border-color: #524C4C; background-color: #a0a0a0;}
.custom_popupContent .custom_popupBtnsContainer input[type='button']:first-child:hover{background-color:#22961A;}
.custom_popupContent .custom_popupBtnsContainer input[value='Cancel']:hover{background-color:#999;}
.custom_popupContent .custom_popupBtnsContainer input[type='button']:focus{border-color: #3ff;}
.custom_popupContent input.inputTexts{width: 98%;height: 24px;line-height: 24px;background-color: #f3f3f3;border:1px solid #d3d3d3;margin-top: 15px;text-indent: 0.5em;font-size: 12px;}
.custom_popupContent input.inputTexts:focus{border-color: #666;}

jquery代码:

 var oUtils = function(){

   //text为弹出的文字信息,timestamp为多长时间弹出框自动消失,callback为回调方法
function _alertTips(text,timestamp,callback){
var autoHideFlag = ((typeof(timestamp) !="undefined") && (timestamp!=null)) ?true:false; createTipsEvent("alert",text,callback,autoHideFlag); var $obj = $("#alert_popupContent");
if($obj.siblings(".custom_popupContent").length>0){
$obj.css("z-index","18");
$obj.prev(".custom_popupFilterBg").css("z-index","18");
}
if(autoHideFlag){
var _timer = setTimeout(removeCustomTips,timestamp);
function removeCustomTips(){
if($("#alert_popupBg").css("display")!= undefined){
if(typeof(callback)!="undefined" && $.isFunction(callback)){
callback();
}
$("#alert_popupBg,#alert_popupContent").fadeOut(1000,function(){
$("#alert_popupBg,#alert_popupContent").remove();
});
}
clearTimeout(_timer);
};
}
} //text——>弹出的文字信息,confirmFun——>点击确认按钮之后执行的方法,cancelFun——>点击取消按钮之后执行的方法
function _confirmTips(text,confirmFun,cancelFun){
createTipsEvent("confirm",text,confirmFun,cancelFun);
} //text——>弹出的文字信息,confirmFun——>点击确认按钮之后执行的方法,cancelFun——>点击取消按钮之后执行的方法
function _promptTips(text,confirmFun,cancelFun){
createTipsEvent("prompt",text,confirmFun,cancelFun);
} function createTipsEvent(typeFlag,text,confirmFun,lastParam){
var operateStr="";
switch(typeFlag){
case "alert":
if(!lastParam){
operateStr = '<div id="'+typeFlag+'_operateBtns" class="custom_popupBtnsContainer">\
<input type="button" value="OK" id="'+typeFlag+'_sureBtn"/>\
</div>';
};
break;
case "confirm":
case "prompt":
operateStr='<div id="'+typeFlag+'_operateBtns" class="custom_popupBtnsContainer">\
<input type="button" value="OK" id="'+typeFlag+'_sureBtn"/>\
<input type="button" value="Cancel" id="'+typeFlag+'_cancelBtn"/>\
</div>';
break;
}; var _html = '<div id="'+typeFlag+'_popupBg" class="custom_popupFilterBg"></div>\
<div id="'+typeFlag+'_popupContent" class="custom_popupContent">\
<div id="'+typeFlag+'_tipsText" class="custom_popupTipsText"></div>'+
(typeFlag == "prompt"?'<input type="text" name="inputMsg" class="inputTexts"/>':'')
+operateStr+
'</div>'; $("body").prepend(_html);
$("#"+typeFlag+"_tipsText").html(text);
var $Obj = $("#"+typeFlag+"_popupContent");
$Obj.animate({
filter: "alpha(opacity=100)",
opacity:"1",
marginLeft:-($Obj.width()/2),
marginTop:-($Obj.height()/2)
},300); switch(typeFlag){
case "alert":
case "confirm":
$("#"+typeFlag+"_operateBtns input[value='OK']").focus();
break;
case "prompt":
$Obj.find("input[name='inputMsg']").focus();
break;
} $("#"+typeFlag+"_operateBtns").off("click","#"+typeFlag+"_sureBtn");
$("#"+typeFlag+"_operateBtns").on("click","#"+typeFlag+"_sureBtn",function(){
switch(typeFlag){
case "alert":
case "confirm":
if(typeof(confirmFun)!="undefined" && $.isFunction(confirmFun)){
confirmFun();
}
closeTips($(this).parent().parent(".custom_popupContent"));
break;
case "prompt":
var _inputMsg = $.trim($Obj.find("input[name='inputMsg']").val());
if(typeof(confirmFun)!="undefined" && $.isFunction(confirmFun)){
if(confirmFun(_inputMsg)){
closeTips($(this).parent().parent(".custom_popupContent"));
};
}
break;
}
}); $("#"+typeFlag+"_operateBtns").off("click","#"+typeFlag+"_cancelBtn");
$("#"+typeFlag+"_operateBtns").on("click","#"+typeFlag+"_cancelBtn",function(){
closeTips($(this).parent().parent(".custom_popupContent"));
if(typeof(lastParam)!="undefined" && $.isFunction(lastParam)){
lastParam();
}
});
} function closeTips(obj){
$(obj).prev(".custom_popupFilterBg").remove();
$(obj).remove();
} return{
alertTips:function(text,timestamp,callback){
_alertTips(text,timestamp,callback);
},
confirmTips:function(text,confirmFun,cancelFun){
_confirmTips(text,confirmFun,cancelFun);
},
promptTips:function(text,confirmFun,cancelFun){
_promptTips(text,confirmFun,cancelFun);
}
}
}();

demo实例:

 oUtils.alertTips("Please input the page number.",200,test);

 //弹出框在0.2s后自动消失,然后调用test()方法,第二个和第三个参数是可选的

  oUtils.confirmTips("Would you like to delete this service?",confirmFun,cancelFun);

 oUtils.promptTips("Please fill the email here:",function(email){
if(email==""){
// 什么都没输入
oUtils.alertTips("Email cannot be empty.");
return false;
}else{
//输入后点击确认执行操作的地方 ......    return true;
}
},cancelFun);

自定义alert,confirm,prompt事件,模仿window.alert(),confirm(),prompt()的更多相关文章

  1. Console.log,Window.alert,Document.write三者区别

    1.Console.log不会阻断程序继续进行,在控制台可以看到测试结果. 2.Window.alert弹出框会阻断程序运行,在弹出框可以看到测试结果. 3.Document.write不会阻断程序继 ...

  2. window.alert弹出处理

    # -*- coding:utf-8 -*- """ window.alert 处理 """ from selenium import we ...

  3. javascript学习笔记(window .alert 是什么)

    <script language="javascript"> var abc="25"; window .alert(abc); </scri ...

  4. 网站开发进阶(二十)JS中window.alert()与alert()的区别

    JS中window.alert()与alert()的区别 前言 alert与window.alert没什么区别,如果有人觉得有区别,那就来解释一下:所有以window.开始的语句,都可以直接把wind ...

  5. ASP.NET MVC下使用AngularJs语言(四):$window.alert

    判断文本框是否有填写,没有填写使用angularjs的$window.alert来提示用户. 创建一个ASP.NET MVC控制器: 接下来是准备一个angularjs的控制器: pilotApp.c ...

  6. 不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方window.alert=function(aa){ if (typeof (aa)"undefined"){ throw "就是这";}};

    不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方 var oldalert=window.alert; window.alert=function(aa){ i ...

  7. 获取 js DOM元素中绑定的所有事件,模仿 chrome getEventListeners

    偶尔看到了这个问题,如何用JS获取元素某一事件上绑定的所有Listener? 突然觉得好像是有解决办法的,查了下,在 chrome 下,支持 window.getEventListeners(obj) ...

  8. 测开之路九十九:js函数、事件、window窗体对象

    函数:function 函数名(参数列表) 事件 单击:onclick()表单提交:onsubmit()鼠标经过:onmouseover()值改表时:onchange() window窗体对象转跳:w ...

  9. JavaScript DOM编程基础精华01(DOM入门,DOM模型和获取页面元素,事件,window对象的方法)

    DOM入门 DOM就是Html页面的模型,将每个标签都做为一个对象,JavaScript通过调用DOM中的属性.方法就可以对网页中的文本框.层等元素进行编程控制.比如通过操作文本框的DOM对象,就可以 ...

随机推荐

  1. C#操作AD的例子

    一下连接中包含了使用c#对AD操作的各种列子 http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-D ...

  2. HDU 5729 - Rigid Frameworks

    题意:    对于一个由n*m个1*1的菱形组成可任意扭曲的矩形(姑且这么说),求添加斜线*(两种)让菱形变成正方形,使得整个矩形固定且无法扭曲的方案数. 分析:    n*m的矩形有如下性质:( 平 ...

  3. Intel 编译Boost库

    C:\Windows\SysWOW64\cmd.exe /E:ON /V:ON /K ""C:\Program Files (x86)\Intel\Composer XE 2013 ...

  4. django 使用jquery ajax post数据问题

    django 开启了CSRF功能导致jquery ajax post数据报错 解决方法在post数据里引入csrfmiddlewaretoken: '{{ csrf_token }}'},同时需要在f ...

  5. 实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域

    首先肯定要先看看这两部分是不是有内存重叠?为什么? 1.因为如果有内存重叠(目的地址起始位置处于源指针所指区域之中),你再从起始位置复制的话,这样目的地址改变的时候将源地址内存里面存的东西给改变了,所 ...

  6. Nginx 配置指令的执行顺序(八)

    前面我们详细讨论了 rewrite.access 和 content 这三个最为常见的 Nginx 请求处理阶段,在此过程中,也顺便介绍了运行在这三个阶段的众多 Nginx 模块及其配置指令.同时可以 ...

  7. 解决:getWeatherbyCityName(city),服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。

    原文:getWeatherbyCityName(city),服务器无法处理请求. ---> 未将对象引用设置到对象的实例. 解决方法:不要直接使用 “服务引用” , 添加为 “Web 引用” 最 ...

  8. Unable to execute dex: Multiple dex files define Lorg/ap (

    解决这个问题的方法,直接把commons-collections.jar这个jar包删除,一定要删干净啊,各个地方看一下,再clean下,应该没问题了!根据这个英文的目录指示就是Unable to e ...

  9. 从Linux终端管理进程:10个你必须知道的命令

    从Linux终端管理进程:10个你必须知道的命令 Linux终端有一系列有用的命令.它们可以显示正在运行的进程.杀死进程和改变进程的优先级.本文列举了一些经典传统的命令和一些有用新颖的命令.本文提到的 ...

  10. .net面试问答(大汇总)

    用.net做B/S结构的系统,您是用几层结构来开发,每一层之间的关系以及为什么要这样分层? 答:从下至上分别为:数据访问层.业务逻辑层(又或成为领域层).表示层 数据访问层:有时候也称为是持久层,其功 ...