【问题标题】:Print button from iframe从 iframe 打印按钮
【发布时间】:2015-01-19 13:04:42
【问题描述】:

我为所有 100 页添加了一个 iframe,并且我想在该 iframe 中添加“打印”按钮。这样每个页面都有一个来自 iframe 的按钮。 但是当点击打印按钮时只打印 iframe 页面。我想打印整个可视页面。

<input type="button" value="Print" onclick="window.self.print();" />

<input type="button" value="Print" onclick="parent.window.focus();" />

我尝试了几个代码,但没有任何效果。

【问题讨论】:

    标签: javascript html iframe


    【解决方案1】:

    首先你可以添加函数:

    function printMe() {
      window.print()
    }
    

    然后你可以从 iframe 的按钮调用:

    <input type="button" value="Print" onclick="printMe();" />
    

    更新

    在与@Manwal 相同的逻辑中,您可以通过 id 或 class 获取容器,然后专注于此。 示例:

    <input type="button" value="Print" onclick="document.getElementById('.container').focus();document.getElementById('.container').print();" />
    

    【讨论】:

    • 首先,您的目标是完成打印。为什么不从 iframe 中取出打印按钮并放入父窗口中?
    • 我无法编辑父窗口,我只能在 iframe 文件中进行更改。
    【解决方案2】:

    试试这个代码:

    <input type="button" value="Print" onclick="parent.window.focus();window.print();" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多