0

我在文字下面有这个来显示我的动态表

<div id="Print">     
    <asp:Literal ID="Literal1" runat="server"></asp:Literal> 
</div> 

我放置一个图像按钮来打印

<asp:ImageButton ID="ImageButton5" runat="server" 
ImageUrl="~/Image/Print_Icon_Design.jpg" Height="25px" 
Width="27px" style="margin-left:20px" OnClientClick="CallPrint"/> 

我有下面的代码要打印,但它不工作你能帮忙吗

<script language="javascript" type="text/javascript">
    function CallPrint(Print) {
        var prtContent = document.getElementById(Print);
        var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
</script>
4

4 回答 4

0

跳过函数的参数 Print 并添加更改 getElementById

var prtContent = document.getElementById('Print');
于 2012-11-07T12:41:11.627 回答
0

您需要Print在引号内加上您的函数缺少最后的大括号。此外,您不需要 Print 参数(因为您没有从 ImageButton 传递它并且您已经使用 getElementById() 检索它:

function CallPrint() {
            var prtContent = document.getElementById("Print");
            var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
            WinPrint.document.write(prtContent.innerHTML);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
}
于 2012-11-07T12:41:27.663 回答
0

试试这个

<script language="javascript" type="text/javascript">

function CallPrint() {
                var prtContent = document.getElementById('Print');
                var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
                WinPrint.document.open();
                WinPrint.document.write(prtContent.innerHTML);
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                return false;
}
 </script>

<asp:ImageButton ID="ImageButton5" runat="server" 
                        ImageUrl="~/Image/Print_Icon_Design.jpg" Height="25px" 
            Width="27px" style="margin-left:20px" OnClientClick="return CallPrint()"/>

返回 false 到 OnClientClick 将阻止回发

于 2012-11-07T12:43:02.740 回答
0

prtContent.innerHTML 将为空,因为您使用的是 Print 而不是 'Print'

        function CallPrint() {
        var prtContent = document.getElementById('Print');
        var htmlcontent = prtContent.innerHTML;
        var WinPrint = window.open('', '', 'left=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(htmlcontent);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
    }
于 2012-11-07T12:49:45.510 回答