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 onclick='closeBox()' onmouseover=/"this.style.background='#ffffff';this.style.color='#333333'/"  onmouseout=/"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 onmouseover=/"this.style.background='#0099FF';this.style.color='#ffffff'/" onmouseout=/"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 onmouseover=/"this.style.background='#0099FF';this.style.color='#ffffff'/" onmouseout=/"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' onclick='closeBox()'>取 消</span></td></tr></table>";
 }else{
  str+="<span onmouseover=/"this.style.background='#0099FF';this.style.color='#ffffff'/" onmouseout=/"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").onclick=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 onclick='closeBox()' onmouseover=/"this.style.background='#ffffff';this.style.color='#333333'/"  onmouseout=/"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 onmouseover=/"this.style.background='#0099FF';this.style.color='#ffffff'/" onmouseout=/"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").onclick=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()方法來調用,刪除成功之後會自動重新加載當前頁,具體效果如圖片

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章