有多篇文章涉及这些主题的一部分,但没有一篇将它们放在一起。将JavaScript和HTML添加到IFRAME中,并将IFRAME打印到打印机
在给定的页面上,我希望创建一个新的IFRAME,添加一些JavaScript将它打印到打印机,并添加一些HTML将打印。
以下是我在http://jsbin.com/UDUCADI/2/进行现场演示的尝试。它打印,但打印父页面,而不是IFRAME。
我该如何打印IFRAME?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Print IFRAME</title>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#printIt').click(function(){
var iframe = $('<iframe>');
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "window.print()";
iframe[0].appendChild(script);
iframe.appendTo('body');
var iframeDocument=$(iframe[0].contentWindow.document);
iframeDocument.find('body').html($('#stuffToPrint').html());
})
});
</script>
</head>
<body>
<button id="printIt">Print</button>
<div id="stuffToPrint">
<p>Print this text!</p>
<img alt="And print this image" src="dropdown_arrow_blue.gif">
</div>
<div>Don't print this!</div>
</body>
</html>
隐藏一切以仅打印样式表? – Blazemonger