javascript - 使用链接按钮打印 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;
}
我想在单击 LinkButton 而不是单击 <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 按钮,打印功能不会被调用。任何帮助将不胜感激。
解决方案
推荐阅读
- sql - 如何获取表中每一列的空值计数
- c# - 由于 IDbAsyncEnumerable 错误,无法使用实体框架查询数据库
- asp.net-mvc - ASP.NET MVC4 - 渲染部分问题
- r - install.packages 不处理文件路径中的空格
- android - Android Service 获取Binder包
- javascript - 单击页面上的其他位置时折叠移动标题
- c - 访问列表项的内存地址的操作顺序?
- java - 将 Liferay 7.x 与 Oracle 11g2 外部数据库连接 - NullPointerException
- codeigniter - Codeigniter 在数据库中提交多行
- javascript - Vue模板调试