jQuery扩展:模拟原生alert与confirm对话框实现

作者:阿沐

时间:2026-10-25

浏览:451

本文介绍如何使用jQuery扩展方法模拟浏览器原生的alert和confirm对话框。通过$.extend定义全局方法,结合Dialog单体模式生成DOM结构,实现自定义样式的提示框。代码包含HTML模板、CSS样式及事件绑定逻辑,展示了从创建对话框到绑定确认/取消事件的完整实现过程。

文章目录

jQuery模拟alert与confirm对话框实现

效果展示


jQuery模拟alert与confirm对话框实现

完整代码


具体实现如下:

 
/** 
* @author xing
*/ 
(function($){
$.extend({
alert:function(html,callback){
var dialog=new Dialog();
dialog.build('alert',callback,html);
}, 
confirm:function(html,callback){
var dialog=new Dialog();
dialog.build('confirm',callback,html);
} 
}); 
var Dialog=function(){
var render={
template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="确定" id="sure"/></div></div></div>',
templateConfirm:' <div class="alertParent" id="confirmPanel"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="取消" id="cancel"/><input type="button" value="确定" id="sure"/></div></div></div>',
/** 
* 根据类型生成对话框
* @param {Object} type
* @param {Object} callback
* @param {Object} html
*/ 
renderDialog:function(type,callback,html){
if(type=='alert'){
this.renderAlert(callback,html);
}else{
this.renderConfirm(callback,html);
} 
}, 
/** 
* 生成alert对话框
* @param {Object} callback
* @param {Object} html
*/ 
renderAlert:function(callback,html){
var temp=$(this.template).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('alert',temp,callback);
}, 
/** 
* 生成confirm对话框
* @param {Object} callback
* @param {Object} html
*/ 
renderConfirm:function(callback,html){
var temp=$(this.templateConfirm).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('confirm',temp,callback);
}, 
/** 
* 设定对话框的位置
* @param {Object} el
*/ 
setPosition:function(el){
var width=el.width(),
height=el.height(),
pageSize=this.getPageSize();
el.css({
top:(pageSize.h-height)/2,
left:(pageSize.w-width)/2
}); 
}, 
/** 
* 绑定事件处理
* @param {Object} type
* @param {Object} el
* @param {Object} callback
*/ 
bindEvents:function(type,el,callback){
if(type=="alert"){
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
}); 
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
}); 
} 
}else{
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
}); 
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
}); 
} 
$('#cancel').click(function(){
$('div.alertParent').remove();
}); 
} 
}, 
/** 
* 获取页面尺寸
*/ 
getPageSize:function(){
return {
w:document.documentElement.clientWidth,
h:document.documentElement.clientHeight
} 
} 
} 
return {
build:function(type,callback,html){
render.renderDialog(type,callback,html);
} 
} 
} 
})(jQuery);

由于alert无需选择器支持,采用$.extend声明


具体实现如下:

 
$.extend({
alert:function(html,callback){
}, 
confirm:function(html,callback){
} 
}); 

声明单体生成组件,返回公共方法build创建组件


具体实现如下:

 
var Dialog=function(){
return {
build:function(type,callback,html){
render.renderDialog(type,callback,html);
} 
} 
} 

分别声明组件的HTML字符串模板


具体实现如下:

 
var render={<BR> template:' <div class="alertParent"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!
</div><div class="btnBar"><input type="button" value="确定" id="sure"/></div></div></div>',<BR> templateConfirm:' <div class="alertParent"
id="confirmPanel"><div class="alertContent"><h2 class="title">系统提示</h2><div class="alertHtml">你的操作出现错误!</div><div class="btnBar"><input type="button" value="取消"
id="cancel"/><input type="button" value="确定" id="sure"/></div></div></div>'}<BR>

向内部填充方法逻辑


具体实现如下:

 
/** 
* 根据类型生成对话框
* @param {Object} type
* @param {Object} callback
* @param {Object} html
*/ 
renderDialog:function(type,callback,html){
if(type=='alert'){
this.renderAlert(callback,html);
}else{
this.renderConfirm(callback,html);
} 
}, 
/** 
* 生成alert对话框
* @param {Object} callback
* @param {Object} html
*/ 
renderAlert:function(callback,html){
var temp=$(this.template).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('alert',temp,callback);
}, 
/** 
* 生成confirm对话框
* @param {Object} callback
* @param {Object} html
*/ 
renderConfirm:function(callback,html){
var temp=$(this.templateConfirm).clone().hide();
temp.find('div.alertHtml').html(html);
$(document.body).append(temp);
this.setPosition(temp);
temp.fadeIn();
this.bindEvents('confirm',temp,callback);
}, 
/** 
* 设定对话框的位置
* @param {Object} el
*/ 
setPosition:function(el){
var width=el.width(),
height=el.height(),
pageSize=this.getPageSize();
el.css({
top:(pageSize.h-height)/2,
left:(pageSize.w-width)/2
}); 
}, 
/** 
* 绑定事件处理
* @param {Object} type
* @param {Object} el
* @param {Object} callback
*/ 
bindEvents:function(type,el,callback){
if(type=="alert"){
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
}); 
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
}); 
} 
}else{
if($.isFunction(callback)){
$('#sure').click(function(){
callback();
$('div.alertParent').remove();
}); 
}else{
$('#sure').click(function(){
$('div.alertParent').remove();
}); 
} 
$('#cancel').click(function(){
$('div.alertParent').remove();
}); 
} 
}, 
/** 
* 获取页面尺寸
*/ 
getPageSize:function(){
return {
w:document.documentElement.clientWidth,
h:document.documentElement.clientHeight
} 
} 

对话框实现部分


具体实现如下:

 
$.extend({
alert:function(html,callback){
var dialog=new Dialog();
dialog.build('alert',callback,html);
}, 
confirm:function(html,callback){
var dialog=new Dialog();
dialog.build('confirm',callback,html);
} 
}); 

调用方法示例


具体实现如下:

 
$.confirm('确定删除?',function(){
alert('cccc');
}); 

jQuery模拟alert与confirm对话框实现

$.alert('我的电脑');


jQuery模拟alert与confirm对话框实现

最后是CSS与HTML结构


具体实现如下:

 
div.alertParent{
padding:6px;
background:#ccc;
background:rgba(201,201,201,0.8);
width:auto;
position:absolute;
-moz-border-radius:3px;
font-size:12px;
top:50px;
left:50px;
} 
div.alertContent{
background:#fff;
width:350px;
height:auto;
border:1px solid #999;
} 
h2.title{
width:100%;
height:28px;
background:#0698E9;
text-indent:10px;
font-size:12px;
color:#fff;
line-height:28px;
margin:0;
} 
div.alertHtml{
background:url(dtips.gif) 0 0 no-repeat;
height:50px;
margin:10px;
margin-left:30px;
text-indent:50px;
line-height:50px;
font-size:14px;
} 
div.btnBar{
border-top:1px solid #c6c6c6;
background:#f8f8f8;
height:30px;
} 
div.btnBar input{
background:url(sure.png) no-repeat;
border:0;
width:63px;
height:28px;
float:right;
margin-right:5px;
} 

html


具体实现如下:

 
<div class="alertParent"><BR><div class="alertContent"><BR><h2 class="title">系统提示</h2><BR><div class="alertHtml"><BR>你的操作出现错误!<BR></div><BR> <div class="btnBar"><BR> <input
type="button" value="确定"/><BR></div><BR></div><BR> </div><BR>

高手勿笑,为说明实现方式,代码未完善,仅半小时内写出,存在纰漏,以笨方式实现模拟alert,下次将通过构建Button方式实现组件,加入遮罩,ajax调用,iframe宽度高度自适应等更强大功能。

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章