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

标签 javascript jquery html iframe

我有一个 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>

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

关于javascript - 使用 javascript 从另一个 div 打印 div 或 iframe 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34149327/

相关文章:

javascript - 动态更改图标

javascript - 无法使用 EventListener 对未安装的组件执行 React 状态更新

javascript - 如何去掉下面 JavaScript 循环中的逗号?

jquery - 如何向 jQuery Mobile 中的元素添加填充

php - 在使用 where 条件后如何在 laravel 5.4 的表中获取没有重复值的单列值

javascript - 如何将 .hasClass 与 $(this) 一起使用?

javascript - 在 Ext 中使用 XTemplate 和 SimpleStore

javascript - 循环中的变量

javascript - 为什么找不到带有 '$all' 修饰符的文档?

javascript - 如何在单个页面上多次使用音频播放器?