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#中的peek()方法

    peek()方法用来判断文件是否读取完成,如果完成的话,就会有一个返回值 - 1 所以可以用streamreader的对象sr调用peek()方法来判断文件流是否读取完成 ) { Console.Wr ...

  2. Linux学习之fsck命令

    在windows下,磁盘的文件系统出错,需要运行chkdsk命令进行修复.而在linux下,则需要运行fsck命令.由于linux对于文件系统的错误非常敏感,由于意外断电或者其它原因导致linux系统 ...

  3. 在controller间分享数据(第一种办法)

    Blood 血腥的方法 每个controller都有自己的scope, 同时也可以共享他们老爸的scope内的数据.如果我们想让两个controller共享数据的化, 有多种方法. 最直接血腥的就是在 ...

  4. python操作csv-xls完善后的代码

    #coding:utf-8 #导入相应模块 import csv,xlwt,sys,os,fnmatch,xlrd from xlutils.copy import copy #对xls文件中的绝对值 ...

  5. humble number(hd1058)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  6. 九章算法系列(#4 Dynamic Programming)-课堂笔记

    前言 时隔这么久才发了这篇早在三周前就应该发出来的课堂笔记,由于懒癌犯了,加上各种原因,实在是应该反思.好多课堂上老师说的重要的东西可能细节上有一些急记不住了,但是幸好做了一些笔记,还能够让自己回想起 ...

  7. 五个典型的JavaScript面试题

    问题1: 范围(Scope) 思考以下代码: 1 2 3 4 5 (function() {    var a = b = 5; })();   console.log(b); 控制台(console ...

  8. StringBuilder[] 作为数组如何使用

    在工作中突然要用到这个就记录下来. 不知为何我这里的StringBuilder[] 数组必须要指明几个(les)才给用,否则就会报错. int les = 5; StringBuilder[] sb_ ...

  9. c# 正则表达式对网页进行内容抓取

    原文 c# 正则表达式对网页进行内容抓取 搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容.简单来说,就是吧HTML文本中的HTML标记去掉,留下我们用IE等浏览器打开HTML文档看到的部分(我 ...

  10. ubuntu 下node 环境的安装

    1. 安装nodejs sudo apt-get install nodejs 注意:包名是nodejs 2. 建立node软连接 #找到nodejs位置 which nodejs # /usr/bi ...