【问题标题】:jQuery Colorbox Iframe and SelectorjQuery Colorbox Iframe 和选择器
【发布时间】:2011-01-04 00:46:45
【问题描述】:

我有这个:

$("a.money").colorbox({href:$("a.money").attr("href")+" div#content"});

这会打开叠加层,但仅包含 #content 选择器中的内容。如果我添加 iframe:true,这将不再有效。有没有办法让它与 iframe 一起使用?

编辑: 我可以让它最接近工作的是这样做:

$("a.money").colorbox({iframe:true, width:700, height:425,
    onComplete: function() {
        alert('test');
        $test = $("#cboxLoadedContent iframe").contents().find("#content").html();
        $("#cboxLoadedContent iframe").contents().find("#container").html($test);
    } 
});

虽然没有警报,但它似乎不起作用,但我研究了一下,我认为我需要使用 .live(),这导致我尝试这样做:

$('a.money').live('click', function() { 
    url = this.href; // this is the url of the element event is triggered from
    $.fn.colorbox({href: url, iframe:true,width:700, height:425,
        onComplete: function() {
            $test = $("#cboxLoadedContent iframe").contents().find("#content").html();
            $("#cboxLoadedContent iframe").contents().find("#container").html($test);
        } 
    });
    return false;
});

没用,我仍然需要添加警报才能使其正常工作。

如果您可能想知道我要做什么。网页加载在 iframe 中,所有元素都在 #container 中,因此包括 #header、#sidebars,但我只想在 iframe 中显示 #content。然而,这导致我意识到另一个问题。假设我在没有警报的情况下让它工作,它只适用于那个初始页面。例如,如果我在 iframe 中加载了一个表单,在提交表单后,我将再次看到整个布局,而不仅仅是#content 部分。是否可以在进一步导航时继续仅显示页面的#content 部分?

【问题讨论】:

    标签: jquery iframe colorbox


    【解决方案1】:

    您的初始代码在没有警报的情况下无法运行的原因是,除了需要“onComplete”功能之外,您还需要 iframe 上的加载事件。该警报为 iframe 提供了加载时间,从而使您的代码正确呈现。

    所以,它应该看起来像:

    $("a.money").colorbox({iframe:true, width:700, height:425,
        onComplete: function() {
            $("#cboxLoadedContent iframe").load(function(){
                  $test = $(this).contents().find("#content").html();
                  $(this).contents().find("#container").html($test);
        });
        } 
    });
    

    【讨论】:

      【解决方案2】:

      这应该可行。

        $("a.money").colorbox({ href:"#content", inline: true, iframe: true });
        <div id='content'>Any content here</div>
        <a class='money' href="#">question</a>
      

      【讨论】:

      • 对不起,如果我不太清楚,我实际上需要 iframe 中的 #content ,它正在从另一个页面加载内容。所以我需要这样的东西:$("a.money").colorbox({href:$("a.money").attr("href")+" div#content", iframe:true, width:600, height:400 });(但这当然行不通)
      • 试过了,但它不起作用,因为它不是真正的内联内容。
      【解决方案3】:

      我根据这些线程中的代码找到了替代解决方案:

      http://groups.google.com/group/colorbox/browse_thread/thread/9a54137b8cae96c5/11d37ea7ab72562f http://groups.google.com/group/colorbox/browse_thread/thread/8a0deec97202e27c/2e7524b87931a89e

      它不使用 iframe,但它或多或少正是我想要的。

      【讨论】:

      • 仅供参考 - 对于从 Google 搜索来到这里的任何人,上面的“groups.google.com”链接已损坏。
      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 2012-01-29
      • 2012-03-24
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      相关资源
      最近更新 更多