用javascript打印问题…Div是一个空字符串

Printing with javascript problems.....div comes as a null string

本文关键字:一个 字符串 打印 javascript 问题 Div      更新时间:2023-09-26

我正在使用javascript函数来打印页面。我不断得到字符串作为一个空值,我不知道如何.....下面是代码。div我有被称为divSheet和它的设置为可见的false开始…当您加载信息时,它会在divSheet中创建一个表并将其设置为true。有人知道为什么它说在getPrint函数的条纹是空的吗?谢谢你!

            <asp:ImageButton runat="server" ID="imageBtnPrint" Style="z-index: 100" ImageUrl="~/Images/printerIcon.gif"
                               OnClientClick="javascript:getPrint('divSheet')" ToolTip="Print" />

    function getPrint(strid)  
    {
        var pp = window.open();
        var prtContent = document.getElementById(strid);
        pp.document.writeln('<HTML><HEAD><title>Print Confirmation Sheet</title><LINK href=PrintStyle.css  type="text/css" rel="stylesheet">')
        pp.document.writeln('<LINK href=PrintStyle.css  type="text/css" rel="stylesheet" media="print"><base target="_self"></HEAD>')
        pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">');
        pp.document.writeln('<form method="post">');  
        pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right><INPUT ID="PRINT" type="button" value="Print" onclick="javascript:location.reload(true);window.print();"><INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();"></TD></TR><TR><TD></TD></TR></TABLE>');
        pp.document.writeln(document.getElementById(strid).innerHTML);
        pp.document.writeln('</form></body></HTML>');
    }

我认为这是因为你是从主HTML生成子窗口,并且子页面不能直接访问父页面的变量。

你可以在父HTML中设置一个局部变量为div的ID,然后从JS函数中使用:window.opener.<variable name>

访问它

像这样:(警告:未测试)

...
var divId;
function getPrint(strid)  
{ 
    divId = strid;
    var pp = window.open();
    var prtContent = document.getElementById(window.opener.divId);
    ...
    ...
}