如何打印部分页面?
如何在新窗口中显示部分页面?用户打印。
注意:部分页面位于IFrame中。
答案 0 :(得分:2)
打印IFrame的内容时有一些限制。 JavaScript安全策略将阻止您在其他域中打印某些内容。这是我多年来用于在页面上打印特定内容的方法。
首先,您需要在JavaScript中使用简单的打印功能。这是我使用的那个
print = function(str) {
var WinPrint = window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.open("text/html", "replace");
WinPrint.document.write('<html><head><title>Printing</title></head><body>');
WinPrint.document.write(str);
WinPrint.document.write('</body></html>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
然后你需要从iframe
中提取HTML getIFrameContent = function(IFrameID) {
return document.getElementById(IFrameID).contentWindow.document.body.innerHTML;
};
然后一个简单的函数来包装整个事情以使其更容易
printIFrame = function(IFrameID) {
print(getIFrameContent(IFrameID));
};
完成所有操作后,您可以调用printIFrame方法,只需发送IFrame的ID。
方法2: 在新窗口中打开Iframe并打印该窗口。
var printIFrame2 = function (IFrameID) {
var URL = document.getElementById(IFrameID).getAttribute("src");
var WinPrint = window.open(URL, 'WinPrint', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
答案 1 :(得分:1)
除非我提出问题,否则我不知道发布代码。因此,对于Cris来说,这就是将javascript放在主页中这样的内容吗?
<script type="text/javascript">
print = function (str) {
var WinPrint = Window.open('', '', 'left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.open("");
WinPrint.document.write('');
WinPrint.document.write(str);
WinPrint.document.write('</body></html>');
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
};
getIFrameContent = function (IFrameID) {
return document.getElementById(IFrameID).contentWindow.document.body.innerHTML;
};
printIFrame = function (IFrameID) {
print(getIFrameContent(IFrameID));
};
</script>
然后从代码中调用,就像这样......
ScriptManager.RegisterStartupScript(Page, Me.GetType(), "key", "printIFrame(" & MyIFrame.ID & ");", True)
答案 2 :(得分:0)
这是我测试的:
页面的可打印部分:
<div id="printArea">
<h1>I am going to print from here</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
打印按钮:
<a href="#" onClick="printDiv();return false;">Print</a>
Javascript代码:
function printDiv() {
var w = window.open('', '','width=auto,height=auto,resizeable,scrollbars');
w.document.write($("#printArea").html());
w.document.close();
javascript:w.print();
w.close();
return false;
}