javascript+div+css模拟alert效果和confirm效果

/*
参数说明
str:要提示的字符串内容
gotopage:要调用页面的url地址
paras:传递的参数,格式为[para1=xxx&para2=yyy]
returnpage:调用ajax返回处理结果后,原页面重定向到returnpage,如果为空,则页面重载,否则重定向
    若gotopage='alert',则页面不重定向,相当于alert();
iscancel:是否需要取消按钮,0为没有,1为有
*/
function sAlert(str,gotopage,paras,returnpage,isCancel){
    var msgw,msgh,bordercolor,msgInfo;
    msgw=250;//提示窗口的宽度
    msgh=110;//提示窗口的高度
    titleheight=25 //提示窗口标题高度
    bordercolor="#569CE9";//提示窗口的边框颜色
    titlecolor="#6BA3F5";//提示窗口的标题颜色
  
    var sWidth,sHeight;
    sWidth=document.body.clientWidth;
    sHeight=document.body.scrollHeight;
    var bgObj=document.createElement("div");
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position="absolute";
    bgObj.style.top="0";
    bgObj.style.background="#FFFFFF";
    bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    bgObj.style.opacity="0.6";
    bgObj.style.left="0";
    bgObj.style.width=sWidth;
    bgObj.style.height=sHeight;
    bgObj.style.zIndex = "10000";
    document.body.appendChild(bgObj);
  
    var msgObj=document.createElement("div")
    msgObj.setAttribute("id","msgDiv");
    msgObj.setAttribute("align","center");
    msgObj.style.background="#F7F9FF";
    msgObj.style.border="1px solid " + bordercolor;
    msgObj.style.position = "absolute";
 msgObj.style.left = "65%";
 msgObj.style.top = "50%";
 msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
 msgObj.style.marginLeft = "-225px" ;
 msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
 msgObj.style.width = msgw + "px";
 msgObj.style.height =msgh + "px";
 msgObj.style.textAlign = "center";
 msgObj.style.lineHeight ="25px";
 msgObj.style.zIndex = "10001";
  
 var title=document.createElement("h4");
 title.setAttribute("id","msgTitle");
 title.setAttribute("align","right");
 title.style.margin="0";
 title.style.padding="3px";
 title.style.background=bordercolor;
 title.style.opacity="0.75";
 title.style.border="1px solid " + bordercolor;
 title.style.height="18px";
 title.style.font="13px Verdana, Geneva, Arial, Helvetica, sans-serif";
 title.style.color="white";
 //title.style.cursor="pointer";
 title.innerHTML="<span οnclick='closeBox()' οnmοuseοver=/"this.style.background='#ffffff';this.style.color='#333333'/"  οnmοuseοut=/"this.style.background='none';this.style.color='#ffffff'/" style='float:right;cursor:pointer;display:block;color:#ffffff;text-align:center;line-height:15px; width:30px; height:14px;'>关闭</span>";
 document.body.appendChild(msgObj);
 document.getElementById("msgDiv").appendChild(title);
 var txt=document.createElement("p");
 txt.style.margin="1em 0"
 txt.setAttribute("id","msgTxt");
 if(isCancel==1){
  str+="<table><tr><td align=right><span οnmοuseοver=/"this.style.background='#0099FF';this.style.color='#ffffff'/" οnmοuseοut=/"this.style.background='#99CCFF';this.style.color='#333333'/" style='margin-top:6px;cursor:pointer;color:#333333;display:block;text-align:center;line-height:19px; width:45px; height:18px; border:1px solid #99CCFF;background:#99CCFF' id='closeDiv'>确 定</td><td width=30></td><td align=left><span οnmοuseοver=/"this.style.background='#0099FF';this.style.color='#ffffff'/" οnmοuseοut=/"this.style.background='#99CCFF';this.style.color='#333333'/" style='margin-top:6px;cursor:pointer;color:#333333;display:block;text-align:center;line-height:19px; width:45px; height:18px; border:1px solid #99CCFF;background:#99CCFF' id='reseltDiv' οnclick='closeBox()'>取 消</span></td></tr></table>";
 }else{
  str+="<span οnmοuseοver=/"this.style.background='#0099FF';this.style.color='#ffffff'/" οnmοuseοut=/"this.style.background='#99CCFF';this.style.color='#333333'/" style='margin:6px 0 0 0px;cursor:pointer;color:#333333;display:block;text-align:center;line-height:19px; width:45px; height:18px; border:1px solid #99CCFF;background:#99CCFF' id='closeDiv'>确 定</span>";
 }
    txt.innerHTML=str;
    document.getElementById("msgDiv").appendChild(txt);
    
 document.getElementById("closeDiv").οnclick=function(){
  document.body.removeChild(bgObj);
        document.getElementById("msgDiv").removeChild(title);
        document.body.removeChild(msgObj);
   
  if(gotopage!=""&&gotopage!=null){
   //window.location.href=gotopage;
   $.post(gotopage,paras,function(data){
    sResponse(data,returnpage)
   });
  }
 }
}
function closeBox(){
 document.body.removeChild(bgDiv);
 document.getElementById("msgDiv").removeChild(msgTitle);
 document.body.removeChild(msgDiv);
}

/*参数说明:
str: 弹出提示的内容
gotopage:关闭提示后页面转向地址,为空则重载当前页,不为空则重定向到gotopage页
  若gotopage='alert',则页面不重定向,相当于alert();
*/
function sResponse(str,gotopage){
    var msgw,msgh,bordercolor,msgInfo;
    msgw=250;//提示窗口的宽度
    msgh=110;//提示窗口的高度
    titleheight=25 //提示窗口标题高度
    bordercolor="#569CE9";//提示窗口的边框颜色
    titlecolor="#6BA3F5";//提示窗口的标题颜色
  
    var sWidth,sHeight;
    sWidth=document.body.clientWidth;
    sHeight=document.body.scrollHeight;
    var bgObj=document.createElement("div");
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position="absolute";
    bgObj.style.top="0";
    bgObj.style.background="#FFFFFF";
    bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    bgObj.style.opacity="0.6";
    bgObj.style.left="0";
    bgObj.style.width=sWidth;
    bgObj.style.height=sHeight;
    bgObj.style.zIndex = "10000";
    document.body.appendChild(bgObj);
  
    var msgObj=document.createElement("div")
    msgObj.setAttribute("id","msgDiv");
    msgObj.setAttribute("align","center");
    msgObj.style.background="#F7F9FF";
    msgObj.style.border="1px solid " + bordercolor;
 msgObj.style.position = "absolute";
 msgObj.style.left = "65%";
 msgObj.style.top = "50%";
 msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
 msgObj.style.marginLeft = "-225px" ;
 msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
 msgObj.style.width = msgw + "px";
 msgObj.style.height =msgh + "px";
 msgObj.style.textAlign = "center";
 msgObj.style.lineHeight ="25px";
 msgObj.style.zIndex = "10001";
 
 var title=document.createElement("h4");
 title.setAttribute("id","msgTitle");
 title.setAttribute("align","right");
 title.style.margin="0";
 title.style.padding="3px";
 title.style.background=bordercolor;
 title.style.opacity="0.75";
 title.style.border="1px solid " + bordercolor;
 title.style.height="18px";
 title.style.font="13px Verdana, Geneva, Arial, Helvetica, sans-serif";
 title.style.color="white";
 //title.style.cursor="pointer";
 title.innerHTML="<span οnclick='closeBox()' οnmοuseοver=/"this.style.background='#ffffff';this.style.color='#333333'/"  οnmοuseοut=/"this.style.background='none';this.style.color='#ffffff'/" style='float:right;cursor:pointer;display:block;color:#ffffff;text-align:center;line-height:15px; width:30px; height:14px;'>关闭</span>";
 document.body.appendChild(msgObj);
 document.getElementById("msgDiv").appendChild(title);
 var txt=document.createElement("p");
 txt.style.margin="1em 0"
 txt.setAttribute("id","msgTxt");
 str+="<span οnmοuseοver=/"this.style.background='#0099FF';this.style.color='#ffffff'/" οnmοuseοut=/"this.style.background='#99CCFF';this.style.color='#333333'/" style='margin:6px 0 0 0px;cursor:pointer;color:#333333;display:block;text-align:center;line-height:19px; width:45px; height:18px; border:1px solid #99CCFF;background:#99CCFF' id='closeDiv'>确 定</span>";
 txt.innerHTML=str;
 document.getElementById("msgDiv").appendChild(txt);
   
 document.getElementById("closeDiv").οnclick=function(){
  document.body.removeChild(bgObj);
  document.getElementById("msgDiv").removeChild(title);
  document.body.removeChild(msgObj);
  
  if(gotopage!='alert'){
   if(gotopage!=""&&gotopage!=null){
   
   window.location.href=gotopage;
   }
   else{
   location.reload();
   }
  }
 }
}

 

注意:上述两个方法因为使用了Jquery的ajax调用,所以在使用之前一定要先引入jquery的文件方可使用

上述2个方法还可以做为ajax异步刷新调用,比如删除操作都可以使用sAlert()方法来调用,删除成功之后会自动重新加载当前页,具体效果如图片

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值