iframe数据打印不正确

时间:2012-03-13 13:02:23

标签: javascript

我正在尝试使用iframe打印div数据并且div包含带有分页的网格,当我使用js打印div时,只有几个记录打印在.xps文件中..为什么会这样......

这是iframe:

<iframe id='ifrmPr' src='#'style="width:100%;height:100%;display:block;border:none;ackground:transparent">    
</iframe>

这是我打印div的js代码

var ifrm = document.getElementById('ifrmPr');
        var content = document.getElementById('PrintSection').innerHTML;

        /* Determine what DOM model to use */
        var printDoc = (ifrm.contentWindow || ifrm.contentDocument);
        if (printDoc.document) {
            printDoc = printDoc.document;
        }

        /* Create a HTML document to go into the iFrame */
        /* The title will appear on the printed document */
        printDoc.write("<html><head><title>title</title>");
        printDoc.write("<link rel='stylesheet' type='text/css' href='../../Content/pageElement.css' />");
        printDoc.write("<link rel='stylesheet' type='text/css' href='../../Content/Site.css'  />");
        printDoc.write("<link rel='stylesheet' type='text/css' href='../../Content/style.css'  />");
        printDoc.write("</head><body onload='this.focus(); this.print();'>");
        printDoc.write(content + "</body></html>");
        printDoc.close();

1 个答案:

答案 0 :(得分:0)

try this method而非使用.write()

var ifrm = document.getElementById('ifrmPr');

var printDoc = ifrm.contentWindow || ifrm.contentDocument;
if (printDoc.document) {
    printDoc = printDoc.document;
}

var frameBody = printDoc.getElementsByTagName('body')[0];

frameBody.innerHTML = "test";

我认为(不确定),this是窗口/文档对象。我认为你可以从父框架调用printDoc.print()而不是在子框架中调用它。