Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript打印多个网页iFrame内容(全部打印按钮)_Javascript_Loops_Iframe_Printing_Document - Fatal编程技术网

Javascript打印多个网页iFrame内容(全部打印按钮)

Javascript打印多个网页iFrame内容(全部打印按钮),javascript,loops,iframe,printing,document,Javascript,Loops,Iframe,Printing,Document,使用JavaScript,如何创建“全部打印”按钮 无论何时单击“全部打印”按钮,都会遍历iFrame的不同src并打印内容 请注意,iFrame当前设置在主网页上。有导航按钮可以更改iFrame的内容src。此主网页的设置类似于使用导航按钮浏览幻灯片内容。导航按钮实际上是导航到不同的网页 因此,我猜测内容需要附加到文档或排列,以便使用“全部打印”按钮一次打印所有内容 我成功地用以下代码打印了“当前幻灯片”(或iFrame内容): function PrintCurrentSlide() {

使用JavaScript,如何创建“全部打印”按钮

无论何时单击“全部打印”按钮,都会遍历iFrame的不同src并打印内容

请注意,iFrame当前设置在主网页上。有导航按钮可以更改iFrame的内容src。此主网页的设置类似于使用导航按钮浏览幻灯片内容。导航按钮实际上是导航到不同的网页

因此,我猜测内容需要附加到文档或排列,以便使用“全部打印”按钮一次打印所有内容

我成功地用以下代码打印了“当前幻灯片”(或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;
}
window.onload=function(){
window.frames[“printf”].focus();
window.frames[“printf”].print();
}
函数打印(){
var newWin=window.frames['printf'];
newWin.document.write('这是我插入的新页面');
newWin.document.close();
}
函数更改(){
变量url=”http://www.apple.com/";
变量$iframe=$('#ifrm');
如果($iframe.length){
$iframe.attr('src',url);
返回false;
}
返回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
}


}

pages=[]//在此处启动空列表
函数printPage(){
var frames=document.getElementsByTagName('iframe');
//获取所有的iframe并在其上循环
//然后将他们的innerHTML推到列表中
对于(var i=0;i
使用此解决方案,您甚至可以避免iframe超过一页时被切断的问题

请记住,在父页面HTML中,您将使用下面的代码调用printPage函数

<input type="submit" value="Print All"
  onclick="javascript:printPage()"
 />


谢谢你,普拉布。我花了一段时间才理解你提供的代码,因为我是个新手。但是,我确实尝试了代码。它似乎多次弹出打印对话。最后,我决定采用不同的方法让页面打印内容会更容易。我想让你知道我是多么感激你的帮助。@Sharma,你能告诉我们你使用的不同方法吗。我陷入了类似的境地,我似乎无法摆脱它。谢谢