1

无法将动态内容设置为在 fancybox 的 onComplete 方法中动态创建的 iframe。静态 iframe 也是如此。如何在运行时将动态内容设置为 iframe?

$(document).ready(function() {
    $("a").fancybox({
        'titleShow': false,
        'autoScale': true,
        'hideOnOverlayClick': false,
        'centerOnScroll': true,
        'scrolling': 'no',
        'content': $('div#content').html(),
        'onComplete': function() {
            //method #1
            $('<iframe id="testiframe" src="about:blank" style="border: 0px;"/>').load(function() {
                $('#testiframe').contents().find('body').append('test').width("100%");
            }).appendTo('div#iframe');

            //method #2
            $('<iframe id="testiframe"/>').appendTo('div#iframe').attr("style", "border: 0px;");
            $('#testiframe').contents().find('body').append('test');

            var iframe = document.getElementById('testiframe');
            if (iframe) {
                iframe.width = "100%";
                iframe.height = "";
                iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
            }

            //method #3
            $('#testiframe3').contents().find('body').append('test').width("100%");
        }
    });
});

http://jsfiddle.net/9xszx/

4

1 回答 1

0

也许我正试图将文本放入 iframe,但为时已晚,无法对其进行任何更改,但我能够让它像这样工作:

$(document).ready(function() {
    $("a").fancybox({
        'titleShow': false,
        'autoScale': true,
        'hideOnOverlayClick': false,
        'centerOnScroll': true,
        'scrolling': 'no',
        'content': function() {
            return $('div#content').html() + '<iframe id="iframeTemplate" frameborder="0" vspace="0" hspace="0" src="about:blank"></iframe>';
        },
        'onComplete': function() {
            var iframeElem = document.getElementById('iframeTemplate');
            var iframeDoc = (iframeElem.contentWindow.document || iframeElem.contentDocument); iframeDoc.open();
            iframeDoc.write('<html><head><title></title><b>test</b></body></html>');
            iframeDoc.close();
        }
    });
});
于 2012-10-19T11:53:01.287 回答