【问题标题】:Callback from the parent when colorbox is closed from iframe从 iframe 关闭颜色框时从父级回调
【发布时间】:2011-12-13 18:44:21
【问题描述】:

我知道您可以在使用关闭按钮时使用颜色框进行回调:

onClosed:function(){ alert('onClosed: colorbox has completely closed'); }

但是,在成功的 ajax 请求之后,我正在从 iframe 本身关闭颜色框,例如:

if(json.status == 'success') {
parent.$j.fn.colorbox.close();
}

我的问题是如何通知父窗口颜色框已关闭,然后从那里运行回调?这可能吗?

【问题讨论】:

    标签: javascript jquery colorbox


    【解决方案1】:

    onClosed 事件即使在你的 iframe 中关闭颜色框时也会被触发,并且它已经在父级的上下文中被触发。所以你可以把你的回调放在 onClosed 函数中:

    $("a.pic").colorbox({
        onClosed: function() {
            myCallback;
            //do other stuff
        },
        //or if everything is in the callback:
        onClosed: myCallback,
        href:"myDog.jpg"
    });
    

    就通知而言,如果所有内容都在您的回调中,这就是您所需要的。如果你有更多的事情要做并且不能把所有的东西都放到那个回调中,你可以使用 jQuery 的触发器来触发另一个“关闭”事件:

    $.colorbox({
        onClosed: function() {
            $(window).trigger("colorboxClosed");
        },
        href:"myDog.jpg"
    });
    

    然后将函数绑定到该自定义事件:

    $(window).bind("colorboxClosed", function() {
        alert("closed");
    });
    

    如果您使用的是 jQuery 1.7+ 并且不需要向后兼容,建议您使用 on 函数而不是 bind

    【讨论】:

    • 嗯.. 我发誓我试过 =P 现在效果很好。感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2012-09-15
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多