js原生alert弹框美化

1、说明

通过自定义方式美化弹框,使用alert弹框错误提示

2、示例

html代码片段:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>welcome</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="/assets/js/jquery.js"></script>
    <script type="text/javascript" src="/assets/js/front.js?v=1"></script>
    <style type="text/css">
        /*弹窗*/
        div{padding: 0;  margin: 0;}
        .cover{background: rgba(0,0,0,0.64);width: 100%;height: 100%;position: fixed;top: 0;left: 0;z-index: 101;}
        .pop_box{width: 74%;margin: 0 auto;position: fixed;top: 30%;left: 50%;margin-left: -37%;z-index: 102;}
        .pop_img img{width: 100%;}
        .pop_center{background: url(/assets/images/img/pop_center.png) no-repeat;min-height: 44px; margin-left: -9px;}
        .pop_center span{text-align: center;font-size: 18px;color: #fff;line-height: 30px; margin-left: 28%;}
    </style>
</head>
<body>
    用户名:<input type="text" name="username" id="username" />
    <br /><br />
    性&nbsp;&nbsp;&nbsp;别:<input type="radio" name="sex" value="1" />&nbsp;
            <input type="radio" name="sex" value="2"/>
    <br /><br />
    <input type="button" value="submit" onclick="check();" />
</body>
</html>

front.js

//弹出自定义提示窗口
var showAlert= function(msg, url){
    //弹框存在
    if ( $("#alert_box").length > 0) {
        $('#pop_box_msg').html(msg);
    } else {
        var alertHtml = '<div id="alert_box">'
                    +       '<div class="cover"  id="cover_alert"  onclick="closeAlert()"></div>'
                    +       '<div class="pop_box" id="pop_box_alert" onclick="closeAlert()">'
                    +           '<div class="pop_img">'
                    +               '<img src="/assets/images/img/pop_top.png">'
                    +           '</div>'
                    +           '<div class="pop_center">'
                    +               '<span id="pop_box_msg">' + msg + '</span>'
                    +           '</div>'
                    +           '<div class="pop_img">'
                    +               '<img src="/assets/images/img/pop_bottom.png">'
                    +           '</div>'
                    +       '</div>'
                    +   '</div>';
        $("body").append(alertHtml);
    }
    $("#alert_box").show();
    if(url){
         setTimeout(function(){
            window.location.href = url + '?id=' + 10000*Math.random();
         } , 2000 );
    }else{
         setTimeout("$('#alert_box').hide();" , 2000);
    }
}

//重定义alert
window.alert=showAlert;

//点击遮罩关闭
function closeAlert(){
    $("#alert_box").hide();
}

//验证表单
function check()
{
    var username = $.trim($("#username").val());
    if (username.length < 1) {
        alert('用户名不能为空!');
        return false;
    }
    var sex = $('input[name="sex"]:checked').val();
    if (sex.length < 1) {
        alert('性别不能为空!');
        return false;
    }
    alert('提交成功!');
    return true;
}

3、效果

这里写图片描述


1)本示例使用laravel框架,php语法
2)弹框样式可根据实际情况自行定义

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了美化js alert弹窗,我们可以使用第三方插件或者自己编写样式来实现。以下是一些常用的方法: 1. SweetAlert2插件:这是一个非常流行的弹窗插件,可以轻松创建漂亮的弹窗效果。它支持多种类型的弹窗,包括警告、错误、成功、信息等等。你可以通过它的官网下载,并按照文档进行配置和使用。 2. 自定义样式:如果你不想使用第三方插件,也可以自己编写css样式,来美化alert弹窗。你可以通过修改弹窗的背景色、字体、边框、阴影等等,来实现你想要的效果。 以下是一个简单的例子,展示如何通过自定义样式美化alert弹窗: ``` <style> .alert { background-color: #fff; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2); padding: 20px; font-size: 16px; color: #333; } .btn-ok { background-color: #007bff; color: #fff; border: none; padding: 10px 20px; border-radius: 5px; margin-top: 10px; cursor: pointer; } </style> <script> function myAlert() { var msg = "Hello World!"; var div = document.createElement('div'); div.classList.add('alert'); div.innerHTML = msg; var btn = document.createElement('button'); btn.classList.add('btn-ok'); btn.innerText = 'OK'; btn.onclick = function() { div.parentNode.removeChild(div); } div.appendChild(btn); document.body.appendChild(div); } </script> <button onclick="myAlert()">Click me</button> ``` 上面的代码会在点击按钮时,弹出一个自定义样式alert弹窗,包含一条信息和一个确认按钮。你可以根据需要修改样式,来实现你想要的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值