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. GET & POST 登录

    GET 登录 @property(nonatomic,assign)long  long hasReceivedContentLength; - (void)getLogin { NSString * ...

  2. C#中Spli、正则表达式分解字符串详解

    一.String.Split方法提供了如下6个重载函数: 名称 说明 String.Split (Char[]) 返回包含此实例中的子字符串(由指定 Char 数组的元素分隔)的 String 数组. ...

  3. 基于excel9.h的excel处理

    基于excel9.h的excel处理; #include "excel9.h" #include <iostream> using namespace std; cla ...

  4. js监控视频播放的事件并打印log

    html代码: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" ...

  5. Ajax 用法, 实现方法,JS原生与JQ实现

    AJAX 详解 ajax是实现页面异步加载. 常用于, 前后端数据交互, 实现前端页面无刷新更改操作. 是web前端和后端使用者开发的必备使用技能~~ Ajax操作~   :  俗话原理 : 用俗话来 ...

  6. Python之路第七天,基础(9)-面向对象(上)

    面向对象的编程思想 回想 我们所学过的编程方法: 面向过程:根据业务逻辑从上到下写堆叠代码. 函数式编程:将重复的代码封装到函数中,只需要写一遍,之后仅调用函数即可. 面向过程编程最易被初学者接受,其 ...

  7. MySQL命令记录1

    mysql命令行 开启:net start mysql56关闭:net start mysql56(这两种情况必须有管理员权限) 登陆:mysql -h localhost -u root -p(lo ...

  8. 烧饼(nyoj779)

    描述烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一次可以同时放入k个兰州烧饼,一分钟能做好一面.而现在有n个兰州烧饼,至少需要多少分钟才能 ...

  9. nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。

    ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...

  10. jsp 2种include标签的区别

    众所周知,jsp中有2种标签用于包含其他jsp或者文件 1.include指令,其实是java代码 <%@ include file="xxx.jsp"%> 2.jsp ...