jquery - append 到动态 iframe- jQuery - 在 Firefox 中不起作用

标签 jquery iframe append

火狐16/17

以下代码是我编写的用于打印元素的 jquery 插件。

这在 Firefox 中是有效的,但现在只显示一个空白页面,头部和正文都是空的。这适用于 IE 和 Chrome。

github: https://github.com/jasonday/jquery.printThis

演示: http://jsfiddle.net/jasonday/Tx4Uv/12/

代码:

(function($) {
    var opt;

    $.fn.printThis = function (options) {
        opt = $.extend({}, $.fn.printThis.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);

    // if Opera, open a new tab
        if ($.browser.opera)
        {
            var tab = window.open("","Print Preview");
            tab.document.open();

            var doc = tab.document;
        }
    // add dynamic iframe to DOM
        else
        {
        var strFrameName = ("printThis-" + (new Date()).getTime());

            var $iframe = $("<iframe id='" + strFrameName +"' src='about:blank'/>");

            if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }

            $iframe.appendTo("body");

            var $doc = $("#" + strFrameName).contents();
        }
    // allow iframe to fully render before action
    setTimeout ( function () {

        // import page css
        if (opt.importCSS)
        {
                $("link[rel=stylesheet]").each(function(){
                var href = $(this).attr('href');
                if(href){
                        var media = $(this).attr('media') || 'all';
                        $doc.find("head").append("<link type='text/css' rel='stylesheet' href='" + href + "' media='"+media+"'>");
                    }
        });
        }

        // add another stylesheet
        if (opt.loadCSS)
        {
        $doc.find("head").append("<link type='text/css' rel='stylesheet' href='" + opt.loadCSS + "'>");

        }

        //grab outer container
        if (opt.printContainer) { $doc.find("body").append($element.outer()); }
        else { $element.each( function() { $doc.find("body").append($(this).html()); }); }

        //$doc.close();
        // print
        ($.browser.opera ? tab : $iframe[0].contentWindow).focus();
        setTimeout( function() { ($.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);

        //removed iframe after 60 seconds
        setTimeout(
        function(){
        $iframe.remove();
        },
        (60 * 1000)
        );
    }, 333 );
    }


    $.fn.printThis.defaults = {
        debug: false, //show the iframe for debugging
        importCSS: true, // import page CSS
        printContainer: true, // grab outer container as well as the contents of the selector
        loadCSS: "" //path to additional css file
    };


    jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
    }
})(jQuery);

最佳答案

修复已实现。

$doc 移至 setTimeout 函数 内,从而解决了该问题。

 setTimeout ( function () {

        if ($.browser.opera)
            {
        var $doc = tab.document;
        } else
        {
        var $doc = $("#" + strFrameName).contents();
        }
...

代码也在 github 上更新。

关于jquery - append 到动态 iframe- jQuery - 在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537784/

相关文章:

r - 如何将组行 append 到数据框中

javascript - WebKit 中带有滚轮的生涩视差

jquery - 在提交表单之前和之后在灯箱内显示不同的内容

javascript - 如何随机化段落中的字间距?

python - 应用引擎上的 Facebook 应用在 Canvas iframe 上显示为空白页面

Javascript 重新加载 iframe

python - 从另一个 DataFrame.describe() 创建 DataFrame - Pandas

php - Jquery/Mysql : use mysql data for Auto Complete search tags in jquery?

javascript - 如何使用JS调用iframe中的函数?

javascript - 如何将我用 js append 的内容居中并替换它