随笔 - 27, 文章 - 0, 评论 - 29, 阅读 - 51238
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

简单的Web页面提示框(html + jQuery)

Posted on   吕霖  阅读(4679)  评论(4编辑  收藏  举报
       我们在web页上操作数据时,常常需要在客户端弹出提示信息,常用的提示框是alert函数弹出的,现在我们做一个对用户友好一点的提示框,先发个图片看看效果:


该提示框可设置4个属性:
1背景颜色;
2提示内容;
3显示多长时间:
4关闭后执行的函数:
代码如下:
//my.common.js
//客户端居中弹出层提示
//msg 提示内容,type 是框的不同背景色,time 是显示多长时间,默认1.5秒,fn是函数名称,如果不需要可以传入null
//'/BillWeb/Content/Images/close.gif'  这个是图片,可以自己指定
function showMsg(msg, type, time, fn){
    $("<div id='divMsg'></div>").appendTo("body");
    $("#divMsg").css("background-color",getColor(type))
    .html("<img id='imgID' src='/BillWeb/Content/Images/close.gif' alt='关闭' style='position:absolute; top:2px; right:2px; cursor:pointer '/>" + msg )
    if(time == null || time == undefined){
        time = 1500;
    }
    $("#imgID").click(function(){
        $("#divMsg").fadeOut("fast", function(){
            $("#divMsg").remove();
            if (fn != null) fn();
        })
    })
    //此处按照需求更改,如果成功则不提示任何信息
    $("#divMsg").fadeIn("fast", function(){
        window.setTimeout(function(){
           $("#divMsg").fadeOut("fast", function(){
               $("#divMsg").remove();
               if (fn != null) fn();
           })
        }, time);
    });
}
//此函数根据传入的不同type,返回不同的背景颜色
function getColor(type){
    var strColor = "#d6ed9c";
    switch (type){
        case "alert":
            strColor = "#f3e9a9";
            break;
        case "success":
            strColor = "#d6ed9c";
            break;
        case "failed":
            strColor = "#f3e9a9";
            break;
        case "wait":
            strColor = "#d6ed9c";
            break;
    }
    return strColor;
}
使用方法:
1、首先在页面中加载jQuery1.3.2.js文件
2、在页面中加入对my.common.js文件的引用
3、在需要调用的时候写代码如下:
a:简单调用
      showMsg("请输入查询关键字!", "alert", 2000);
b:复杂一点的调用:
                    showMsg("新增票据类型成功!","success", 3000, function(){
                        $.ajax({
                            type: "POST",
                            url: "/BillWeb/BillType/BillTypeList",
                            data:"accountBookID=" + accBookID,
                            dataType: "html",
                            success:function(res){
                                if(res.length > 0){
                                    //todo  something
                                }
                            }
                        });
                    });

      希望本文能对您有所帮助!
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示