jQuery Colorbox Iframe和Selector

Jok*_*ker 0 iframe jquery colorbox

我有这个:

$("a.money").colorbox({href:$("a.money").attr("href")+" div#content"});
Run Code Online (Sandbox Code Playgroud)

这会打开覆盖,但只打开#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);
    } 
});
Run Code Online (Sandbox Code Playgroud)

虽然没有警报它似乎不起作用,但我调查了一下,我想我需要使用.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;
});
Run Code Online (Sandbox Code Playgroud)

没有工作,我仍然需要添加一个警报,使其工作.

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

小智 5

你的初始代码在没有警报的情况下无法工作的原因是因为除了需要"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);
    });
    } 
});
Run Code Online (Sandbox Code Playgroud)