解决微信浏览器alert问题的原生js实现

今天遇见微信浏览器alert弹框问题,
怎么可以出现这个 关闭网页 ,好不容易获取的客户就这样流失吗?不! 解决吧,正文如下 ,拿去自用 ,觉得有用请点个赞!






<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <!--响应式的rem布局-->
    <script>document.documentElement.style.fontSize = document.documentElement.clientWidth / 750 * 100 + 'px';</script>
    <style type="text/css">
        #alertBtn {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 60%;
            height: .84rem;
            margin: .24rem auto;
            border-radius: .08rem;
            background: #e32742;
            font-size: .30rem;
            font-weight: 700;
            color: #fbf1b3;
            z-index: 2;
    </style>
    <!--自定义ALERT-->
    <script type="text/javascript">
        var Chef = {
            //body 的宽高
            'bodyH': document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight,
            'bodyW': document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth,
            //动态创建 style 标签添加样式
            'cssStyle': function () {
                var doc = document;
                var style = doc.createElement("style");
                if (style.styleSheet) {// IE
                    style.styleSheet.cssText = arguments[0];
                } else {// w3c
                    var cssText = doc.createTextNode(arguments[0]);
                    style.appendChild(cssText);
                }
                var heads = doc.getElementsByTagName("head");
                if (heads.length) {
                    heads[0].appendChild(style);
                } else {
                    doc.documentElement.appendChild(style);
                }
            },
            // 创建并显示遮罩层
            'createChef': function () {
                if (document.body.getElementsByClassName('Chef_opacity').length == 1) {
                    document.body.removeChild(document.body.getElementsByClassName('Chef_opacity')[0]);
                }
                var div = this.create('div');
                div.style.width = this.bodyW + 'px';
                div.style.height = this.bodyH + 'px';
                div.className = 'Chef_opacity';
                document.body.appendChild(div);
            },
            //alert 框
            'alert': function () {
                // 显示遮罩层
                this.createChef();
                // 创建
                var alertDiv = this.create('div'),
                    alertH2 = this.create('h2'),
                    alertX = this.create('span'),
                    alertP = this.create('p'),
                    alertBDiv = this.create('div'),
                    alertFirm = this.create('button');
                alertX.innerHTML = 'X';
                alertX.className = 'Chef_X';
                // 插号的click事件 什么都不做
                alertX.onclick = function () {
                    alertFirm.onclick();
                }
                // 确定按钮的click事件 什么都不做
                alertFirm.onclick = function () {
                    document.getElementsByClassName('Chef_opacity')[0].style.display = 'none';
                    document.body.removeChild(alertDiv);
                }

                //样式以及内容
                alertDiv.className = 'Chef_alert';
                if (arguments.length == 1) {
                    document.getElementsByClassName('Chef_opacity')[0].style.background = 'rgba(0,0,0,' + arguments[0].shade + ')';
                    alertDiv.style.top = arguments[0].offset;
                    if (arguments[0].width == undefined) {
                        alertDiv.style.width = '4.6rem';
                    } else {
                        alertDiv.style.width = arguments[0].width;
                        alertDiv.style.marginLeft = '-' + parseInt(arguments[0].width) / 2 + 'px';
                    }
                    arguments[0].title == undefined ? alertH2.innerHTML = '来自网页的信息' : alertH2.innerHTML = arguments[0].title;
                    arguments[0].content == undefined ? alertP.innerHTML = '' : alertP.innerHTML = arguments[0].content;
                    arguments[0].firm == undefined ? alertFirm.innerHTML = '确定' : alertFirm.innerHTML = arguments[0].firm;
                } else {// -- 默认提示信息
                    alertH2.innerHTML = '来自网页的信息';
                    alertFirm.innerHTML = '确定';
                }
                // 添加到页面
                alertBDiv.appendChild(alertFirm);
                // alertH2.appendChild(alertX);
                alertDiv.appendChild(alertH2);
                alertDiv.appendChild(alertP);
                alertDiv.appendChild(alertBDiv);
                document.body.appendChild(alertDiv);
            },
            //创建
            'create': function () {
                return document.createElement(arguments[0]);
            }
        };
        ;(function (Chef) {
            var cssString = '\
                    *{padding:0;margin:0;}\
                    .Chef_opacity{display:block;background:rgba(0,0,0,0.8);position:fixed;top:0;z-index:99;}\
                    .Chef_alert{position:fixed;top:50%;left:50%;width:4.6rem;border-radius:.08rem;margin:-2rem 0 0 -2.3rem;background:white;z-index:100;text-align: center;font-size:.18rem}\
                    .Chef_alert>h2{width:90%;margin:10px auto;margin-bottom:0;font-size:.24rem;}\
                    .Chef_alert>p{width:100%;margin:0 auto;padding:.25rem 0;border-bottom:1px solid #d8d8d8;font-weight:700;}\
                    .Chef_alert>div{height: .6rem; margin: .20rem auto;}\
                    .Chef_alert>div>button{width:50%;height:100%;border:0;outline:0;font-size:.20rem;color:#e32742;background:none;}\
                    .Chef_X{float:right;font-size:13px;color:grey;cursor:pointer;font-weight:normal;}\
                    ';
            Chef.cssStyle(cssString);
        })(Chef);
    </script>
</head>
<body>
<button id='alertBtn'>alert弹窗</button>
<script type="text/javascript">

    document.getElementById('alertBtn').onclick = function () {
//调用 Chef.alert() 方法
        Chef.alert({
            'title': '',
            'content': '成功!',
            'firm': '确定',
        });
    }

</script>

</body>

</html>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值