3

让我解释。我使用 Colorbox 加载 HTML 文件并将其显示在 iframe 中。HTML 文件包含<title>标签。是否可以使用该<title>标签作为 Colorbox 弹出窗口的标题?我可以使用onComplete事件,但如何?

编辑:我应该澄清代码:

父 HTML:

<!doctype html>
<html>
    <head>
        <title>This is parent</title>
        <script src="jquery.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('a.cb').colorbox({
                    iframe : true,
                    title  : function() { /* here goes the code */ },
                });
            });
        </script>
    </head>
    <body>
        <a class="cb" href="popup.html">Popup</a>
    </body>
</html>

子 HTML:

<!doctype html>
<html>
    <head>
        <title>The title</title>
    </head>
<body>
    ...
</body>
</html>

基本上我希望 Colorbox 显示子 HTML 代码的标题。

4

3 回答 3

5

根据文档,可能无法实现。如果 iframed 文档与您的父文档在同一个域中,您应该能够执行以下操作:

$(document).ready(function(){
    $('a.cb').colorbox({
        iframe : true,
        fastIframe: false,
        onComplete: function(){
            var title = $('.cboxIframe')[0].contentWindow.document.title;
            $('#cboxTitle').text(title);
        }
    });
});
于 2012-06-27T18:09:06.970 回答
1

很难说,但我的猜测是

var titleText = $(fetchedHTML).find('title').text();
于 2012-06-27T09:21:41.377 回答
-1

我不确定我是否很好地理解了你的问题,如果我离主题很远,请告诉我删除我的帖子:)

您可以通过函数设置颜色框标题,在该函数中您可以获得 iframe 标题并将其设置为颜色框标题

$("mycolorbox").colorbox({rel: 'gal', title: function(){
    var title = $( "#frame_id").contents( ).find( "title").html( );
    return 'title';
}});

我不确定代码是否完全正确,但它可能会有所帮助:)

于 2012-06-27T09:35:20.257 回答