简体   繁体   English

使用javascript从另一个div打印div或iframe内容

[英]print div or iframe content from another div using javascript

I have a PHP web page. 我有一个PHP网页。 On the web page i have a iframe and few DIV. 在网页上,我有一个iframe和几个DIV。 Now I want a single print button, which will print the content of the current active window. 现在,我需要一个打印按钮,它将打印当前活动窗口的内容。 If no iframe or div is open then it will print the main page else the current iframe source or div content using javascript. 如果没有打开iframe或div,则它将使用javascript打印主页,否则将显示当前iframe源或div内容。

is it possible? 可能吗?

Here is an sample to print an element. 这是打印元素的示例。 Hope this will help you to get an idea. 希望这会帮助您获得想法。

<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>

The data variable should be replaced with what ever that you want to print. 数据变量应替换为您要打印的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM