自定义alert,confirm,prompt事件,模仿window.alert(),confirm(),prompt()
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()的更多相关文章
- Console.log,Window.alert,Document.write三者区别
1.Console.log不会阻断程序继续进行,在控制台可以看到测试结果. 2.Window.alert弹出框会阻断程序运行,在弹出框可以看到测试结果. 3.Document.write不会阻断程序继 ...
- window.alert弹出处理
# -*- coding:utf-8 -*- """ window.alert 处理 """ from selenium import we ...
- javascript学习笔记(window .alert 是什么)
<script language="javascript"> var abc="25"; window .alert(abc); </scri ...
- 网站开发进阶(二十)JS中window.alert()与alert()的区别
JS中window.alert()与alert()的区别 前言 alert与window.alert没什么区别,如果有人觉得有区别,那就来解释一下:所有以window.开始的语句,都可以直接把wind ...
- ASP.NET MVC下使用AngularJs语言(四):$window.alert
判断文本框是否有填写,没有填写使用angularjs的$window.alert来提示用户. 创建一个ASP.NET MVC控制器: 接下来是准备一个angularjs的控制器: pilotApp.c ...
- 不知道哪里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 ...
- 获取 js DOM元素中绑定的所有事件,模仿 chrome getEventListeners
偶尔看到了这个问题,如何用JS获取元素某一事件上绑定的所有Listener? 突然觉得好像是有解决办法的,查了下,在 chrome 下,支持 window.getEventListeners(obj) ...
- 测开之路九十九:js函数、事件、window窗体对象
函数:function 函数名(参数列表) 事件 单击:onclick()表单提交:onsubmit()鼠标经过:onmouseover()值改表时:onchange() window窗体对象转跳:w ...
- JavaScript DOM编程基础精华01(DOM入门,DOM模型和获取页面元素,事件,window对象的方法)
DOM入门 DOM就是Html页面的模型,将每个标签都做为一个对象,JavaScript通过调用DOM中的属性.方法就可以对网页中的文本框.层等元素进行编程控制.比如通过操作文本框的DOM对象,就可以 ...
随机推荐
- 使用dbstart 和dbshut 脚本来自动化启动和关闭数据库
使用dbstart 和dbshut 脚本来自动化启动和关闭数据库:1. 登录用户root.2. 编辑你的平台的oratab 文件.打开文件/etc/oratab:文件里数据库条目为以下格式:SID:O ...
- 伪元素”:after” , “:before"
伪元素就是源码html中不存在,而视觉上又存在的元素 简单用法: blockquote:before { content: open-quote; // 其他样式 } // ...
- java之集合类框架的简要知识点:泛型的类型擦除
这里想说一下在集合框架前需要理解的小知识点,也是个人的肤浅理解,不知道理解的正不正确,请大家多多指教.这里必须谈一下java的泛型,因为它们联系紧密,我们先看一下这几行代码: Class c1 = n ...
- Java三大特征之多态(三)
面向对象编程有三大特性:封装.继承.多态. 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 继承 ...
- 向Oracle数据库中CLOB插入数据报错问题
今天在项目中向数据库的CLOB属性插入一段篇文章(1000~2000)字就会报一个字符串过长的错误. 网上说用流来处理,没有这么做.这像是一个Bug,只要把插入的数据,默认扩充到2000以上就ok了. ...
- 给WebApp加一个“壳”,实现Andriod系统添加到桌面
IOS系统的Safari浏览器有一个“添加到桌面”的功能,能在手机桌面上为你的Webapp添加一个快捷方式,其外观和Native App看起来一样. 这个功能对Webapp来说太有用了,它能让用户像“ ...
- python之列表、字典的使用
一.概述:以后你在Linux里面写Python脚本的时候会经常用到Python列表.字典,因为你在以后写脚本的时候,大多数情况下都是对文件进行操作处理,使用字典和列表可以很好的操作文件,得出你想要的结 ...
- 原生Javascript 省市区下拉列表插件
每个电商网站中,都会有收件地址管理模块,用户进行地址操作时,最好不要用户手工进行填写地址. 常见的地址管理界面: 实现插件:PCASClass.js 插件下载地址:http://pan.baidu.c ...
- Ruiy classicsQuotations
1,IT界,许多人会称自己为菜鸟,而每只菜鸟都会有鹰的梦想; 2,把做十件事精力用来做一件事情,你事业就经典了;
- react-native 自己搭建热更新服务器
使用到的框架是 react-native-update react-native-update-cli 这个应该执行热更新的时候的终端命令. 通过这个,自己搭建一个热更新的服务器.