Javascript 打印多个网页 iFrame 内容(全部打印按钮)
Javascript Print Multiple Webpages iFrame Content (Print All Button)
使用 JavaScript,如何创建"全部打印"按钮?
每当单击时,"全部打印"按钮都会遍历iFrame的不同src并打印内容。
请注意,iFrame 当前设置在主网页上。 有导航按钮可以更改iFrame的内容源。 此主网页的设置类似于使用导航按钮浏览幻灯片内容。 导航按钮实际上是导航到不同的网页。
因此,我猜测需要将内容附加到文档或排列,以便可以使用"全部打印"按钮一次打印所有内容。
我成功地使用以下代码打印了"当前幻灯片"(或 iFrame 内容):
function PrintCurrentSlide()
{
var el = document.getElementById('ifMain');
el.contentWindow.focus();
el.contentWindow.print();
return;
}
现在,我正在寻找一个答案,只需单击一下即可浏览iFrame src以打印内容。
在你的脚本中试试这个
window.onload=function() {
window.frames["printf"].focus();
window.frames["printf"].print();
}
function print() {
var newWin = window.frames['printf'];
newWin.document.write('<body onload=window.print()>This is a new page I inserted</body>');
newWin.document.close();
}
function change(){
var url="http://www.apple.com/";
var $iframe = $('#ifrm');
if ( $iframe.length ) {
$iframe.attr('src',url);
return false;
}
return true;
}
和在 HTML 中
<input type="button" onclick="print()" value="Test print"/>
<button onclick="change()">next</button>
<iframe id="printf" name="printf" src="dfgdf.html"></iframe>
在这里动态放置您的页面并打印。 使用您的逻辑自动或单击或其他方式进行打印
我想
出了一个解决方法。这样,您只会得到一个打印对话框。
目的是通过使用javascript操作DOM来将所有iframe的内容获取到父窗口中。这是代码:
<script>
pages =[] // initiate an empty list here
function printPage() {
var frames = document.getElementsByTagName('iframe');
// get all the iframes and loop over them
// then push their innerHTML into the list
for (var i = 0; i < frames.length; i++){
pages.push(frames[i].contentWindow.document.body.innerHTML);
;
}
if (pages && pages.length) {
// this if statement, just checks to ensure our list is not empty before running the code.
// here is the magic, we now set the parent window to be equal to all the concatenated iframes innerHTML
window.content.document.body.innerHTML = pages;
// then we print this new window that contains all the iframes
window.print();
}
else {
// do nothing
}
}
使用此解决方案,您甚至可以避免iframe在超过一页时被切断的问题。
请记住,在您的父页面 HTML 中,您将拥有以下代码来调用 printPage 函数。
<input type="submit" value="Print All"
onclick="javascript:printPage()"
/>
相关文章:
- 使用javascript将动态表从一个html页面打印到另一个html页
- JavaScript打印功能使日历停止工作
- esri javascript异步打印
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- jQuery/JavaScript在线公文包表单-打印样式表
- 如何打印嵌套对象的所有值
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- JavaScript-切换“;全部检查”;复选框true/false
- 打印预览没有应用程序页眉和页脚的html表格
- 正在打印图表上的文本
- Rhino打印功能
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- 如何用javascript打印php数组
- Javascript,从静态函数中打印全局对象
- 试图打印/调用/或提醒数组中的对象:Getting Undefined
- Javascript 打印多个网页 iFrame 内容(全部打印按钮)
- 高图表:“全部打印”按钮
- 打印模式窗口的全部内容
- 如何使用window.print()打印具有水平和垂直滚动条的DIV的全部内容
- 我怎么能得到这些数组值全部打印到html表单