javascript写的ajax的对话框

本文介绍了一个使用JavaScript实现的自定义对话框组件,详细展示了如何创建对话框的各个部分,包括背景、空白区域、标题栏及按钮等,并提供了丰富的自定义选项如按钮数量和事件绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//msgbox.js

function MsgBox()//对话框对象
{
    /// <summary>显示一个对话框的类</summary>
    base = this;
    // 背景
    var backDiv = document.createElement("div");
    backDiv.setAttribute("id", "Curllion_MsgBox_Background");
    document.body.appendChild(backDiv);
    this.BackDiv = backDiv;
    with (backDiv.style)
    {
        position = "absolute";
        left = "0px";
        top = "0px";
        display = "none";
        width = "100%";
        height = "100%";
        verticalAlign = "middle";
        textAlign = "center";
        zIndex = "9999";
        backgroundColor = "#FFFFFF";
    }
    // 空白,使对话框居中
    var blankDiv = document.createElement("div");
    backDiv.appendChild(blankDiv);
    this.BlankDiv = blankDiv;
    with (blankDiv.style)
    {
        height = "30%";
        width = "100%";
        backgroundColor = "#FFFFFF";
        display = "none";
    }
    //对话框主体
    var mainDiv = document.createElement("div");
    backDiv.appendChild(mainDiv);
    mainDiv.setAttribute("id", "Curllion_MsgBox");
    this.MainDiv = mainDiv;
    with (mainDiv.style)
    {
        width = "320px";
        //height = "240px";
        borderWidth = "thin";
        borderStyle = "outset";
        backgroundColor = "#D4D0C8";
        cursor = "default";
        textAlign = "center";
        zIndex = "9999";
        display = "none";
    }
    //标题栏背景
    var titleBackDiv = document.createElement("div");
    mainDiv.appendChild(titleBackDiv);
    titleBackDiv.setAttribute("id", "Curllion_MsgBox_Title_Background");
    this.TitleBackDiv = titleBackDiv;
    with (titleBackDiv.style)
    {
        width = "100%";
        backgroundColor = "#000066";
        color = "#FFFFFF";
        marginBottom = "10px";
        display = "none";
    }
    //标题栏文字
    var titleDiv = document.createElement("div");
    titleBackDiv.appendChild(titleDiv);
    titleDiv.setAttribute("id", "Curllion_MsgBox_Title");
    titleDiv.innerText = "标题";
    this.TitleDiv = titleDiv;
    with (titleDiv.style)
    {
        width = "95%";
        backgroundColor = "#000066";
        color = "#FFFFFF";
        fontWeight = "bolder";
        styleFloat = "left";
        paddingRight = "0px";
        marginRigth = "0px";
        textAlign = "left";
        display = "none";
    }
    //标题栏按钮
    var closeDiv = document.createElement("div");
    titleBackDiv.appendChild(closeDiv);
    closeDiv.setAttribute("id", "Curllion_MsgBox_Close");
    closeDiv.innerText = "×";
    this.CloseDiv = closeDiv;
    with (closeDiv.style)
    {
        width = "5%";
        backgroundColor = "#000066";
        color = "#FFFFFF";
        fontWeight = "bolder";
        styleFloat = "left";
        paddingLeft = "0px";
        marginLeft = "0px";
        display = "none";
    }
    closeDiv.onclick = function() { base.close(); }
    //对话框内容
    var contentDiv = document.createElement("div");
    mainDiv.appendChild(contentDiv);
    contentDiv.setAttribute("id", "Curllion_MsgBox_Content");
    this.ContentDiv = contentDiv;
    with (contentDiv.style)
    {
        width = "100%";
        height = "100px";
        textAlign = "center";
        verticalAlign = "middle";
        marginBottom = "10px";
        display = "none";
    }
    //按钮背景
    var buttonsDiv = document.createElement("div");
    mainDiv.appendChild(buttonsDiv);
    buttonsDiv.style.width = "150px";
    buttonsDiv.style.height = "30px";
    buttonsDiv.style.display = "none";
    this.ButtonsDiv = buttonsDiv;
    //第一按钮
    var button1 = document.createElement("div");
    buttonsDiv.appendChild(button1);
    button1.innerText = "确定";
    this.Button1 = button1;
    with (button1.style)
    {
        width = "60px";
        height = "22px";
        borderWidth = "thin";
        borderStyle = "outset";
        backgroundColor = "#D4D0C8";
        textAlign = "center";
        styleFloat = "left";
        display = "none";
    }
    this.Button1.onclick = function()
    {
        base._close();
        if (base.onButton1Click != null)
            base.onButton1Click.apply(base);
        for (var i = 0; i < base.button1ClickEventHandles.length; i++)
        {
            if (base.button1ClickEventHandles[i] != null)
                base.button1ClickEventHandles[i].apply(this);
        }
        base.dispose();
    };
    //第二按钮
    var button2 = document.createElement("div");
    buttonsDiv.appendChild(button2);
    button2.innerText = "取消";
    this.Button2 = button2;
    with (button2.style)
    {
        width = "60px";
        height = "22px";
        borderWidth = "thin";
        borderStyle = "outset";
        backgroundColor = "#D4D0C8";
        textAlign = "center";
        styleFloat = "right";
        display = "none";
    }
    this.Button2.onclick = function()
    {
        base._close();
        if (base.onButton2Click != null)
            base.onButton2Click.apply(base);
        for (var i = 0; i < base.button2ClickEventHandles.length; i++)
        {
            if (base.button2ClickEventHandles[i] != null)
                base.button2ClickEventHandles[i].apply(this);
        }
    };   
    //默认事件
    this.onclose = null;
    this.onshow = null;
    this.onButton1Click = null;
    this.onButton2Click = null;
    //事件监听组
    this.closeEventHandles = new Array();
    this.showEventHandles = new Array();
    this.button1ClickEventHandles = new Array();
    this.button2ClickEventHandles = new Array();
}
MsgBox.prototype.getMessage = function()
{
    return this.ContentDiv.innerText;
};
MsgBox.prototype.setMessage = function(message)
{
    this.ContentDiv.innerText = message;
};
MsgBox.prototype._show = function(message, title, buttons)
{
    var b;
    if (buttons == null)
        b = 2;
    else
        b = buttons;
    //显示div
    if (message != null)
        this.ContentDiv.innerHTML = message;
    if (title != null)
        this.TitleDiv.innerText = title;
    this.BackDiv.style.display = "inline";
    this.BlankDiv.style.display = "inline";
    this.MainDiv.style.display = "inline";
    this.TitleBackDiv.style.display = "inline";
    this.TitleDiv.style.display = "inline";
    //this.CloseDiv.style.display = "inline";
    this.ContentDiv.style.display = "inline";
    //显示按钮
    switch (b)
    {
        case 0:
            this.ButtonsDiv.style.display = "none";
            this.Button1.style.display = "none";
            this.Button2.style.display = "none";
            break;
        case 1:
            this.ButtonsDiv.style.display = "inline";
            this.Button1.style.display = "inline";
            this.Button2.style.display = "none";
            this.ButtonsDiv.style.width = "60px";
            break;
        case 2:
            this.ButtonsDiv.style.display = "inline";
            this.Button1.style.display = "inline";
            this.Button2.style.display = "inline";
            this.ButtonsDiv.style.width = "150px";
            break;
    }
}

MsgBox.prototype.show = function(message, title, buttons)
{
    this._show(message, title, buttons);
    if (this.onshow != null)
    {
        this.onshow.apply(this);
    }
    for (var i = 0; i < this.showEventHandles.length; i++)
    {
        if (this.showEventHandles[i] != null)
            this.showEventHandles[i].apply(this);
    }
};

MsgBox.prototype._close = function()
{
    this.BackDiv.style.display = "none";
    this.BlankDiv.style.display = "none";
    this.MainDiv.style.display = "none";
    this.TitleBackDiv.style.display = "none";
    this.TitleDiv.style.display = "none";
    this.CloseDiv.style.display = "none";
    this.ContentDiv.style.display = "none";
    this.ButtonsDiv.style.display = "none";
    this.Button1.style.display = "none";
    this.Button2.style.display = "none";
};

MsgBox.prototype.close = function()
{
    this._close();
    if (this.onclose != null)
    {
        this.onclose.apply(this);
    }
    for (var i = 0; i < this.closeEventHandles.length; i++)
    {
        if (this.closeEventHandles[i] != null)
            this.closeEventHandles[i].apply(this);
    }
    this.dispose();
};
MsgBox.prototype.attachEvent = function(sEvent, fpNotify)
{
    var tmpEventHandles = null;
    switch (sEvent)
    {
        case "onclose":
            if (this.onclose == fpNotify) return;
            tmpEventHandles = this.closeEventHandles;
            break;
        case "onshow":
            if (this.onshow == fpNotify) return;
            tmpEventHandles = this.showEventHandles;
            break;
        case "onButton1Click":
            if (this.onButton1Click == fpNotify) return;
            tmpEventHandles = this.button1ClickEventHandles;
            break;
        case "onButton2Click":
            if (this.onButton2Click == fpNotify) return;
            tmpEventHandles = this.button2ClickEventHandles;
            break;
        default: break;
    }
    if (tmpEventHandles != null)
    {
        for (var i = 0; i < tmpEventHandles.length; i++)
        {
            if (tmpEventHandles[i] == fpNotify)
                return;
        }
        tmpEventHandles.push(fpNotify);
    }
}
MsgBox.prototype.detachEvent = function(sEvent, fpNotify)
{
    var tmpEventHandles = null;
    switch (sEvent)
    {
        case "onclose":
            if (this.onclose == fpNotify)
            {
                this.onclose = null;
                return;
            }
            tmpEventHandles = this.closeEventHandles;
            break;
        case "onshow":
            if (this.onshow == fpNotify)
            {
                this.onshow = null;
                return;
            }
            tmpEventHandles = this.showEventHandles;
            break;
        case "onButton1Click":
            if (this.onButton1Click == fpNotify)
            {
                this.onButton1Click = null;
                return;
            }
            tmpEventHandles = this.button1ClickEventHandles;
            break;
        case "onButton2Click":
            if (this.onButton2Click == fpNotify)
            {
                this.onButton2Click = null;
                return;
            }
            tmpEventHandles = this.button2ClickEventHandles;
            break;
        default: break;
    }
    if (tmpEventHandles != null)
    {
        for (var i = 0;i < tmpEventHandles.length; i++)
        {
            if (tmpEventHandles[i] == fpNotify)
            {
                tmpEventHandles.splice(i,1);
                return;
            }
        }
    }
}
MsgBox.prototype.dispose = function()
{
    this.ButtonsDiv.removeChild(this.Button1);
    this.ButtonsDiv.removeChild(this.Button2);
    this.MainDiv.removeChild(this.ButtonsDiv);
    this.MainDiv.removeChild(this.ContentDiv);
    this.TitleBackDiv.removeChild(this.CloseDiv);
    this.TitleBackDiv.removeChild(this.TitleDiv);
    this.MainDiv.removeChild(this.TitleBackDiv);
    this.BackDiv.removeChild(this.MainDiv);
    this.BackDiv.removeChild(this.BlankDiv);
    document.body.removeChild(this.BackDiv);
}

//---------------------

//msgbox-vsdoc.js

MsgBox.prototype =
{
    //外观
    BackDiv: {},
    BlankDiv: {},
    MainDiv: {},
    TitleBackDiv: {},
    TitleDiv: {},
    CloseDiv: {},
    ContentDiv: {},
    ButtonsDiv: {},
    Button1: {},
    Button2: {},
    //属性
    infors:
    {
        /// <summary>
        /// 返回信息的节点名
        /// </summary>
    },
    //事件
    onclose: function()
    {
        /// <summary>
        /// 对话框关闭事件
        /// </summary>
    },
    onshow: function()
    {
        /// <summary>
        /// 对话框弹出事件
        /// </summary>
    },
    //方法
    getMessage: function()
    {
        /// <summary>
        /// 返回对话框的信息
        /// </summary>
        /// <returns type="String">返回对话框的信息</returns>
    },
    setMessage: function(message)
    {
        /// <summary>
        /// 设置对话框的信息
        /// </summary>
        /// <param name="message" type="String">
        /// 要显示的信息;
        /// </param>
    },
    show: function(message, title, buttons)
    {
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="message" type="String">
        /// 要显示的信息;
        /// </param>
        /// <param name="title" type="String">
        /// 要显示的标题;
        /// </param>
        /// <param name="buttons" type="Int">
        /// 按钮的个数;
        /// 1表示一个按钮,0或null表示两个按钮,-1表示无按钮
        /// </param>
    },
    _show: function(message, title, buttons)
    {
        /// <summary>
        /// 显示对话框,不响应事件的副本
        /// </summary>
        /// <param name="message" type="String">
        /// 要显示的信息;
        /// </param>
        /// <param name="title" type="String">
        /// 要显示的标题;
        /// </param>
        /// <param name="buttons" type="Int">
        /// 按钮的个数;
        /// 1表示一个按钮,0或null表示两个按钮,-1表示无按钮
        /// </param>
    },
    close: function()
    {
        /// <summary>
        /// 隐藏对话框
        /// </summary>
    },
    _close: function()
    {
        /// <summary>
        /// close方法不响应事件的副本
        /// </summary>
    },
    attachEvent: function(sEvent, fpNotify)
    {
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="sEvent" type="String">
        /// 事件名;
        /// </param>
        /// <param name="fpNotify" type="Function">
        /// 事件响应的方法
        /// </param>
    },
    detachEvent: function(sEvent, fpNotify)
    {
        /// <summary>
        /// 解除事件绑定
        /// </summary>
        /// <param name="sEvent" type="String">
        /// 事件名;
        /// </param>
        /// <param name="fpNotify" type="Function">
        /// 事件响应的方法
        /// </param>
    },
    dispose: function()
    {
        /// <summary>
        /// 消毁对象
        /// </summary>
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值