我有一个PHP网页。在网页上,我有一个iframe和几个DIV。现在,我需要一个打印按钮,它将打印当前活动窗口的内容。如果没有打开iframe或div,则它将使用javascript打印主页,否则将显示当前iframe源或div内容。

可能吗?

最佳答案

这是打印元素的示例。希望这会帮助您获得想法。

<html>
<head>

<input type="button" value="Click to Print" onclick="printDiv()" />

<script type="text/javascript"
src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js">

</script>
<script type="text/javascript">
function printDiv() {
    var data = $('#divToPrint').html()
    var printWin = window.open('', 'Print Preview', 'height=600,width=800');
    printWin.document.write('<html><head><title>Print Preview</title>');
    printWin.document.write('</head><body >');
    printWin.document.write(data);
    printWin.document.write('</body></html>');
    printWin.print();
    printWin.close();
    return true;
}
</script>
</head>
<body>
<div id="divToPrint">jQuery is a fast, small, and feature-rich
    JavaScript library. It makes things like HTML document traversal and
    manipulation, event handling, animation, and Ajax much simpler with an
    easy-to-use API that works across a multitude of browsers.</div>
</body>
</html>


数据变量应替换为您要打印的内容。

10-04 16:12