使用jQuery打印隐藏的iframe

时间:2015-04-08 12:46:07

标签: javascript jquery

我使用jQuery在页面中创建iFrame。然后我将内容附加到iFrame,我想使用jQuery打印该iFrame的内容。我正在使用下面的代码,但它只是试图打印一个空白页。

我做错了什么?

$("#projects").on("click", ".print-row", function() {
        var theForm = $(this).closest('form');
        var rowParent = $(this).closest('tr');
        var id = $(this).closest('tr').attr('id');

        $.post("printProject.php", {id: id}, function(data)
        {
            var data = $.parseJSON(data);
            var table = data[0];
            var type = data.type;

            if(type) {
                $("div#messages").html('<div class="' + type + '-message">Your document will start printing shortly...</div>').slideDown("slow");
            }

            try {
                var $iframe = $('<iframe id="iframePrint">').appendTo('body');
                $iframe.ready(function() {
                    $iframe.contents().find("body").append(table);
                });
                alert($("#iframePrint").get(0));
                $("#iframePrint").get(0).contentWindow.print();
            } 
            catch(e) {
                self.print();
            }
        });

        return false;
});

0 个答案:

没有答案