使用jquery关闭对话框

时间:2012-05-21 18:59:08

标签: jquery

尝试使用esc无法正常关闭此框

$(document).ready(function() {
    $("#btnShowSimple").click(function(e) {
        ShowDialog(false);
        e.preventDefault();
    });

    $("#btnShowModal").click(function(e) {
        ShowDialog(true);
        e.preventDefault();
    });

    $("#btnClose").click(function(e) {
        HideDialog();
        e.preventDefault();
    });



});

function ShowDialog(modal) {
    $("#overlay").show();
    $("#dialog").fadeIn(300);

    if (modal) {
        $("#overlay").unbind("click");
    }
    else {
        $("#overlay").click(function(e) {
            HideDialog();
        });
    }
}

function HideDialog() {
    $("#overlay").hide();
    $("#dialog").fadeOut(300);
}


$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        $('.btnClose').click();
    } // esc
});​

1 个答案:

答案 0 :(得分:2)

为什么不直接拨打HideDialog()功能?

$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        HideDialog();
    } // esc
});​