在我的网页中,我有一个图像,我想用javascript打印它,但JS中图像的id始终为null。请告诉我该怎么做..
<div class="reportFieldsDiv" runat="server" clientidmode="Static">
<table style="width: 400px; margin: 10px 0px 0px 10px; border: 1px solid black">
<tr>
<td>
<img src="~/Images/Layout/TestBadge.jpg" alt="ID Badge" id="mainImage"/>
</td>
</tr>
</table>
<div style="border: 0px solid black;padding:5px 0px 0px 280px">
<asp:Button ID="btnPrint" runat="server" Text="Print" Width="140px" />
</div>
</div>
<script type="text/javascript">
window.onload = function () {
var image = document.getElementById('mainImage');
printImage(image);
};
function printImage(image) {
var printWindow = window.open('', 'Print Window', 'height=400,width=600');
printWindow.document.write('<html><head><title>Print Window</title>');
printWindow.document.write('<img id="mainImage" src=\'..\Images\Layout\TestBadge.jpg');
printWindow.document.write(image.src);
printWindow.document.write('\' /></body></html>');
printWindow.document.close();
printWindow.print();
}
</script>
答案 0 :(得分:1)
您没有将ID添加到printWindow
文档中,如下所示:
printWindow.document.write('<img id="mainImage" src="..\Images\Layout\TestBadge.jpg"');