【问题标题】:Return to iFrame instead of parent on closing Fancybox关闭 Fancybox 时返回 iFrame 而不是父级
【发布时间】:2012-03-25 21:46:51
【问题描述】:

我有一个在 Fancybox 中呈现的 iFrame。在 iFrame 中,我有另一个链接,按照建议 here 在父页面上调用 Fancybox。一切都按预期工作。在关闭使用父页面上的 Fancybox 从 iFrame 中调用的 Fancybox 时,我返回到父页面。但是,我希望将行为修改为返回 iFrame 而不是父页面。

我目前在 iFrame 中的 Fancybox:

jQuery('.container').on('click', 'a.fancybox-large', function(e){
    e.preventDefault();

    parent.jQuery.fancybox({
        href            : this.href,
        padding         : 0,
        closeBtn        : true,
        fitToView       : true,
        helpers         : { overlay: {opacity: 1, css: {'background-color': '#000000'}} }
    });
});

有什么建议吗?

问候, 约翰

【问题讨论】:

    标签: jquery fancybox


    【解决方案1】:

    您可以使用.focus() 方法在iframe 中选择要关注的元素。

    例如,在iframe 中有一个输入字段

    <input type="text" class="field" />
    

    已编辑:添加完整脚本。

    afterClose 选项添加到您的脚本中

    jQuery('.container').on('click', 'a.fancybox-large', function(e){
     e.preventDefault();
     parent.jQuery.fancybox({
      href            : this.href,
      padding         : 0,
      closeBtn        : true,
      fitToView       : true,
      helpers         : { 
       overlay: {
        opacity: 1, 
        css: {'background-color': '#000000'}
       } 
      },
      afterClose: function(){
       jQuery('.input').focus();
      }
     });
    }); // on
    

    fancybox 关闭后,您的注意力将集中在 iframe 内的 input 字段

    【讨论】:

    • 感谢您的回复。我试过你的建议。但是,它会引发错误“尝试在已清除的范围上运行 compile-and-go 脚本”。
    • 奇怪,我测试了它并且工作得很好。您能否分享一个链接以查看错误类型以及这是否与新选项有关? ...还要检查您的选项是否用逗号分隔,但最后一个。以防万一,afterClose 是 fancybox 的 API 选项,因此它应该包含在您的 fancybox 自定义脚本中。
    • 好吧,我有本地安装,所以无法共享链接。新选项上的错误作物和代码中断的行是 jQuery('.input').focus();。我有另一个也使用 afterClose 的 Fancybox 实例。我对 afterClose 没有任何问题。我有正确的逗号。只是为了确认,我将 afterClose 函数添加到我在问题中提到的代码中。我想这就是我应该做的。正确的?其次,我在问题中的代码是在父页面和 iframe 页面上加载的 js 文件的一部分。
    • 更新:我替换了 jQuery('.input').focus();带有警报。同样的问题。
    • 你有没有在 iframe &lt;input class="input" ... 里面的某个地方?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2016-06-30
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多