js创建弹框(提示框,待确认框)

html,body,div,h1,h2,h3,h4,h5,h6,p,table,form,label,ol,ul,li,dl,dt,dd,img,p{margin:0; padding:0;}
html,body{text-size-adjust:none;-webkit-text-size-adjust:none;-webkit-user-select:none;}
a{color:#333; text-decoration:none;}
a,input:focus,div,select{tap-highlight-color:rgba(0,0,0,0);-webkit-tap-highlight-color:rgba(0,0,0,0);}
ul,li{list-style:none;}
.overlay{width: 100%; height: 100%; background: #000; opacity: 0.5; filter: alpha(opacity=50); position: fixed; left: 0; top: 0;z-index: 999;}
.box{width: 300px; height: 300px; background: #ccc; position: fixed; left: 50%; top: 50%;margin:-150px 0 0 -150px;  z-index: 9999; font-size: 18px; color: #000;}
.close{width: 20px; height: 20px; background: url(icon_close.png) scroll center center no-repeat; position: absolute; right: 10px; top: 10px; cursor: pointer;}
<a href="javascript:;" class="btn" id="btn1">提示框</a>
<a href="javascript:;" class="btn" id="btn2">没有关闭按钮的确认框</a>
<a href="javascript:;" class="btn" id="btn3">确认框</a>
var oBtn = document.getElementsByTagName(‘a‘);
var body = document.body || document.getElementsByTagName(‘body‘)[0];
var isClick,timer;
var closeBtn,popus,overlay;
//兼容PC点击事件与手机触屏事件
    if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)){   //苹果手机
        isClick = ‘touchend‘;

    }else if (/(Android)/i.test(navigator.userAgent)){      //安卓手机
        isClick = ‘touchend‘;
    }else {      //pc
        isClick = ‘click‘;
    }
    oBtn[0].onclick = function(){
        showBox({
            el:‘<h1>我1.5秒后消失</h1>‘,
            time:1500
        })
    }
    oBtn[1].onclick = function(){
        showBox({
            el:‘<h1>我没有关闭按钮</h1>‘
        })

    }
    oBtn[2].onclick = function(){
        showBox({
            el:‘<h1>我是h1标签</h1>‘,
            close:true,
            closeFn:function(){
                alert(‘这是个关闭回调函数‘);
            }
        })

    }
    function showBox(init){
        if(!init.el){
            return;
        }
        popus = document.createElement(‘div‘);//弹框内容
        body.appendChild(popus);//body添加弹框
        popus.className = ‘box‘;
        popus.innerHTML= init.el;//弹框添加内容
        if(init.time){          //time代表过一段是时间消失
            clearTimeout(timer)
            timer=setTimeout(function(){
                body.removeChild(popus);//删除弹框内容
            },init.time);
        }else{                 //没time代表需要手动关闭弹框
            overlay = document.createElement(‘div‘);//遮罩层
            body.appendChild(overlay);//body添加遮罩层
            overlay.className = ‘overlay‘;
            //给遮罩层按钮绑定监听事件
            if(overlay.attachEvent){                //IE
                overlay.attachEvent(‘on‘+isClick,isOpen)
            }else{                //标准
                overlay.addEventListener(isClick,isOpen);
            }
            function isOpen(){
                body.removeChild(overlay);//删除遮罩层
                body.removeChild(popus);//删除弹框内容
            }
            if(init.close){               //init.close代表需要关闭按钮
                closeBtn = document.createElement(‘span‘);//弹框关闭按钮
                closeBtn.className = ‘close‘;
                popus.appendChild(closeBtn);//弹框内容添加弹框关闭按钮
                //给关闭按钮绑定监听事件
                if(closeBtn.attachEvent){                //IE
                    closeBtn.attachEvent(‘on‘+isClick,isClose)
                }else{                //标准
                    closeBtn.addEventListener(isClick,isClose);
                }
                function isClose(){
                    body.removeChild(overlay);//删除遮罩层
                    body.removeChild(popus);//删除弹框内容
                    init.closeFn();//调用回调函数
                }
            }
        }
    }
时间: 2024-08-17 13:48:31

js创建弹框(提示框,待确认框)的相关文章

js三种消息框总结-警告框、确认框、提示框

js消息框类别:警告框.确认框.提示框 警告框:alert("文本"); 确认框:confirm("文本"); 提示框:prompt("文本","默认值"); 一:confirm使用范例 <script type="text/javascript"> function test(){ var res = confirm("请选择"); if(res == true){ doc

C# GridView Edit &amp; Delete, 点击Delete的时候弹出确认框

1. 使用GridView自带属性ShowEditButton和ShowDeleteButton,均设为True  <Columns> ... <asp:CommandField ShowEditButton="True" ShowDeleteButton="True"></asp:CommandField> </Columns> 则在GridView指定列的位置会显示Edit和Delete的LinkButton 2.

angular全局确认框confirm

angular.module('custom-template', []).run(["$templateCache", function ($templateCache) { $templateCache.put("template/modal/confirmModelTemplate.html", '<div id="youModel" class="m-c">\n' + ' <div class=&qu

[转]js中confirm实现执行操作前弹出确认框的方法

原文地址:http://www.jb51.net/article/56986.htm 本文实例讲述了js中confirm实现执行操作前弹出确认框的方法.分享给大家供大家参考.具体实现方法如下: 现在在删除或其它操作前弹出确定提示,我们有很多方法,最基本的就是利用js自带的函数confirm来操作了 最简单的用法如下: 鼠标事件使用confirm 复制代码 代码如下: <a href="#" onclick= "if(confirm( '是否确定! ')==false)r

js弹出提示框并跳转页面

1.提示框有两个选择项,点确定跳转,取消停留在原页面ScriptManager.RegisterStartupScript(Page, this.GetType(), "", "<script>if(confirm('请登录?')){location.href='login.aspx'};</script>", false); 2.提示框只有一个确定按钮,跳转到指定页面ScriptManager.RegisterStartupScript(p

JavaScript 中创建三种消息框:警告框、确认框、提示框。

网址:http://www.w3school.com.cn/js/js_popup.asp 警告框 警告框经常用于确保用户可以得到某些信息. 当警告框出现后,用户需要点击确定按钮才能继续进行操作. 语法: alert("文本") 确认框 确认框用于使用户可以验证或者接受某些信息. 当确认框出现后,用户需要点击确定或者取消按钮才能继续进行操作. 如果用户点击确认,那么返回值为 true.如果用户点击取消,那么返回值为 false. 语法: confirm("文本")

确认后再执行的弹出提示框

reCarryoverFunction:function(event,value){ waf.msgBox.showConfirm({ summaryMsg:"是否反结转", buttons:[ {text:"是" ,click: function(){ waf.doPost({ action:"reCarryOverAction", data:{id:ids,signs:signs}, success:function(data){ if(da

js确认框confirm()用法实例详解

先为大家介绍javascript确认框的三种使用方法,具体内容如下 第一种方法:挺好用的,确认以后才能打开下载地址页面.原理也比较清晰.主要用于删除单条信息确认. ? 1 2 3 4 5 6 7 8 9 10 <SCRIPT LANGUAGE=javascript> function del() {  var msg = "您真的确定要删除吗?\n\n请确认!";  if (confirm(msg)==true){   return true;  }else{   retu

Android 学习笔记之AndBase框架学习(二) 使用封装好的进度框,Toast框,弹出框,确认框...

PS:渐渐明白,在实验室呆三年都不如在企业呆一年... 学习内容: 1.使用AbActivity内部封装的方法实现进度框,Toast框,弹出框,确认框...   AndBase中AbActivity封装好了许多方法提供我们去使用,使得在使用的时候更加的方便,只需要传递相关参数即可..省去了我们自己使用基础的函数进行构造...   就好比进度框,Toast框,弹出框,确认框...这些基本的东西都在AndBase的AbActivity封装好了...我们只需要传递参数调用其中内部的方法就可以完成这些视