下面是我在Windows XP的IE 8中运行良好的javascript代码。
<script type="text/javascript">
function printFrame(frameId) {
var iframe = $('#'+frameId)[0];
iframe.contentWindow.focus();
iframe.contentWindow.print();
}
</script上面的函数被调用,然后在父页面中单击“打印框架”按钮。
<iframe id="testFrame" src="" name="testFrame"> </iframe>
<input type="button" onclick="printFrame('testFrame')" value="Print Frame"/>最近,我把我的机器升级到了Windows 7,把IE 8升级到了IE 11。
现在,这个函数并没有给出打印iframe内容的预期结果。我在chrome v34,Firefox30上测试了这个。这似乎在他们中工作,除了在IE 11中。
在研究中,我发现当iframe src被动态设置时,iframe.contentWindow不会返回异常的iframe窗口对象。
我尝试在iframe的jsp中使用window.print()。如果src不是动态更改的,这似乎是可行的。但我需要更改iframe的内容,以便根据父页面中另一个下拉框中的选择进行动态设置。
例如,在IE 11中检查下面的链接。
http://plungjan.name/SO/testprintiframe.html链接第一次在iframe上打印内容时。然后,单击该按钮。这会导致父窗口被发送到打印机,而不是iframe内容。
然后我尝试了另一种方法。在iframe内容jsp中编写了以下代码,并尝试从父页面访问它。我无法访问该方法本身。“iframe jsp中的脚本代码”
<script type="text/javascript">
function printFrame() {
window.print();
}
</script“父jsp中的脚本代码”
尝试1:
<script type="text/javascript">
function printMyFrame(frameId) {
var iframe = $('#'+frameId)[0];
$(iframe).contents().printFrame();
}
</script尝试2:
<script type="text/javascript">
function printMyFrame(frameId) {
setTimeout(function() {
window.frames[frameId].contentWindow.printFrame();
}, 200);
}
</script>我搜索并实现了几乎所有在google搜索中找到的方法来访问iframe窗口,而不是父窗口。但无法使用IE 11打印iframe内容。
请指导我如何在IE 11中打印iframe内容。
发布于 2014-07-25 18:00:45
我最近遇到了类似的问题,并发现需要使用此方法
var target = document.getElementById('print-container');
try {
target.contentWindow.document.execCommand('print', false, null);
} catch(e) {
target.contentWindow.print();
}
});发布于 2016-01-20 17:03:14
这对我很有效,
这在ie、chrome和firefox中都有效。
var result = contentWindow.document.execCommand('print', false, null);
if (!result)
contentWindow.print();发布于 2017-04-18 16:22:17
这基本上是上述答案(Nayas Subramanian)在jsFiddle中的重复-但安全性不允许它在stackoverflow中100%工作……
$('#print').click(function ()
{
var contentWindow = document.getElementById("frameContent").contentWindow;
var result = contentWindow.document.execCommand('print', false, null);
if(!result) contentWindow.print();
});iframe {
width:100%;
height:100%;
border: none;
}
#print {
position:absolute;
left:20px;
top: 20px;
font-size: 20px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="frameContent" name="frameContent" src="https://stackoverflow.com/questions/24673024/unable-to-print-iframe-content-from-parent-page-in-ie-11#"></iframe>
<button id="print">Print</button>
https://stackoverflow.com/questions/24673024
复制相似问题