javascript - 打印iframe内容

标签 javascript jquery html iframe

我想打印 iframe 的内容。我查看了几个例子,但没有一个对我的案例有帮助。我有以下代码

<div id="print-area">
    <iframe src='data:text/html;charset=utf-8,<?php echo $content ?>'></iframe>
</div>

当点击#print按钮时:(此代码打印出div #print-area的内容)

$("#print").click(function (e) {
        var newWindow = window.open();
        newWindow.document.write($('#print-area').html());
        newWindow.print()
    });

代码有效并提示打印窗口,但是当我尝试将 iframe 的内容放入 document.write(...) 时,该功能不起作用。有什么想法吗?

尝试过的解决方案:

Print iFrame content using Javascript

How to Print iframe content

以及其他主题,但未能打印我的 iframe 内容

最佳答案

使用

<iframe id="abcd" src='data:text/html;charset=utf-8,<?php echo "someHtmlContent" ?>'>


            </iframe>
var newWindow = window.open();
                newWindow.document.write($('#abcd').contents().find("html").html());
                newWindow.print()

因为 iframe 是一个独立的页面,它不能被 windows 类访问 这就是为什么使用父类

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

相关文章:

php - 简单的 html dom 解析器表到数组

javascript - 如何停止退格键或删除键来删除光标之前的文本

javascript - 尝试在纯 CSS 视差上使用 JS .scroll()

javascript - Polymer querySelector 在 dom-repeat 中找不到元素

javascript - 如何处理 PhoneGap sql 存储结果?

javascript - 为什么带有 onclick 的按钮内的 img 不触发脚本?

javascript - AngularJS 绑定(bind)在 <img> 标签的 'src' 属性中

javascript - 如何将 Ruby 正则表达式转换为 JavaScript?

javascript - 基于条件的动态行选择

javascript - Dygrahs js(代码): different thickness and color of the line (x-scale)