我正在尝试打印我的应用程序的特定部分。我的div。它适用于html单页,但不适用于我的应用程序。 我的页面是在ASP.NET和c#中,但由于我无法在C#中嵌入该函数,我正在尝试通过javascript。 但是,属性inner.html
存在问题错误:
0x800a138f - JavaScript运行时错误:无法获取属性< innerHTML'未定义或空引用
代码:
<script type="text/javascript">
function printdiv
var prtContent = document.getElementById("specific");
var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
</script>
<asp:Button ID="printButton" runat="server" Text="Print Results" OnClientClick="printdiv()"/>
我的页面在asp中,也许这就是问题所在?我怎样才能使它等同于打印我想要的div?
编辑,我不知道它是否相关:div的特定部分
答案 0 :(得分:0)
使用以下代码。快乐的编码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function PrintDiv() {
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;
}
</script>
</head>
<body>
<form id="form1">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Manoj Bhardwaj</span>
<hr />
<div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
<br />
This is <span style="color: #18B5F0">a sample code my manoj</span>.<br />
i hope this will help you to print a div</span>
</div>
<br />
<input type="button" onclick="PrintDiv();" value="Print" />
</form>
</body>
</html>
我希望这会帮助你。
答案 1 :(得分:0)
您必须将OnClientClick从OnClientClick =“ printdiv()”更改为OnClientClick =“ return printdiv()”
所以您的代码应该做到
<script type="text/javascript">
function PrintDiv() {
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;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Dharmin Shah</span>
<hr />
<div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px" runat="server">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
<br />
This is <span style="color: #18B5F0">Dharmin's sample code</span>.<br />
i hope this will help you to print a div</span>
</div>
<br />
<asp:Button ID="Button" runat="server" Text="Button" OnClientClick="return PrintDiv();" value="Print" />
</form>
</body>