首页 > 解决方案 > 使用链接按钮打印 div ta 的内容

问题描述

我的页面上有以下代码来打印 div 标签的内容:

function PrintDiv() {
            document.getElementById('<%=lblDateTime.ClientID %>').style.display = "block";
             document.getElementById('<%=lblDateTime.ClientID %>').innerHTML = new Date().toLocaleString();

            var contents = document.getElementById("dvContents").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = (frame1.contentWindow) ? frame1.contentWindow : (frame1.contentDocument.document) ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head><title>DIV Contents</title>');
            frameDoc.document.write('</head><body>');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                document.body.removeChild(frame1);
            }, 500);
            return false;
        }

我想在单击 LinkBut​​ton 而不是单击 <input type="button" 时调用此打印。我可以通过单击链接按钮而不是 <input type=button 来调用上述打印功能吗?下面是我的链接按钮:

<asp:LinkButton Width="130px" Height="50px" ID="print" CssClass="btns" runat="server" OnClientClick="printDiv();" >Print</asp:LinkButton>

这是我要打印的内容:

<div>
This is a test  
</div>

这是我现在使用的输入按钮:

  <input type="button" class="btns"  runat="server" onclick="PrintDiv();" value="Print" />

只要我点击 <input type=button,打印功能就会被调用,但是如果我用链接按钮替换 <input type 按钮,打印功能不会被调用。任何帮助将不胜感激。

标签: javascriptjquerycssasp.net

解决方案


推荐阅读