1

Javascript

 function printDiv() {
                var k=document.getElementById("printDiv");
                var el = document.createElement("iframe");
                el.setAttribute('id', 'ifrm');
                el.setAttribute('src', k.innerHTML);
                el.print();

            }

HTML:

<div id="printDiv">
<!-- content Here -->
</div>
<input type="button" value="Print" onclick="printDiv()"/>

代码不起作用。我没有得到任何打印对话框。请帮助我,因为我想使用动态 iframe 打印数据。谢谢。

4

4 回答 4

2

HTML

<iframe name="print_frame" width="0" height="0" frameborder="0" src="about:blank">
        </iframe>

Javascript

 function printDiv(divP) {

            window.frames["print_frame"].document.body.innerHTML = $(divP).html();
            window.frames["print_frame"].window.focus();
            window.frames["print_frame"].window.print();
        }
于 2013-11-28T12:21:28.270 回答
1

替换el.print();

 window.frames.focus();
 window.frames.print();

在 JQuery 中

function printDiv() {

                $('#printDiv').empty().append('<iframe id="ifrm"></iframe>');
                $('#ifrm').attr('width', '300px');
                $('#ifrm').attr('heigh', '300px');
                $('#ifrm').attr('src', 'http://www.amazon.in')
                window.frames.focus();
                window.frames.print();

            }

小提琴演示

http://jsfiddle.net/TBB4N/8/

于 2013-11-07T07:07:07.420 回答
0

iframe 需要焦点,否则它仍然会在 IE 中打印父框架。

function printIframe(id)
{
var iframe = document.frames ? document.frames[id] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
iframe.focus();
ifWin.printPage();
return false;
}
于 2013-11-07T07:01:17.037 回答
0

这个功能应该可以工作

function printInFrame( UrlReport) {

            function closePrint() {
                document.body.removeChild(this.__container__);
            }

            function setPrint() {
                debugger;
                this.contentWindow.__container__ = this;
                this.contentWindow.onbeforeunload = closePrint;
                this.contentWindow.onafterprint = closePrint;
                this.contentWindow.focus(); // Required for IE
                this.contentWindow.print();
            }
            var oHiddFrame = document.createElement("iframe");
            oHiddFrame.onload = setPrint;
            oHiddFrame.style.position = "fixed";
            oHiddFrame.style.right = "0";
            oHiddFrame.style.bottom = "0";
            oHiddFrame.style.width = "0";
            oHiddFrame.style.height = "0";
            oHiddFrame.style.border = "0";
            oHiddFrame.src = UrlReport;
            document.body.appendChild(oHiddFrame);
        }


        /* #endregion */
    }
于 2020-02-12T15:42:52.053 回答