2011-09-14 141 views
1

在这个波纹管程序中,如果你点击'PRINT',html内容将被添加到新的弹出窗口b.html中。它工作正常,但b.html有一些html内容,当新的弹出窗口打开时,我正在丢失这些数据。在不缺少b.html的html内容的情况下,我如何在内部添加新的html内容。新的弹出窗口有问题吗?

我的示例代码:

页面名称:a.html

   <html> 
        <head> 
          <script type="text/javascript" src="js/jquery-1.6.1.min.js"> </script> 
          <script type="text/javascript"> 
           $(document).ready(function() 
           { 
            $("#print").click(function() 
            { 
             var newWind=window.open('b.html', 'Print message',"left=100,screenX=200,menubar=yes, width=980,height=500, location=yes,resizable=yes,scrollbars=yes,status=yes"); 
             var printableHTML=$("#msg").html(); 
             newWind.document.write(printableHTML); 
             //newWind.append(printableHTML); 
            }); 
           }); 
          </script> 
        </head> 
        <body> 
          <span id="print"><u>PRINT</u></span> 

          <div id="msg"> 
          <h4> I am printing this message...</h4> 
          </div> 
        </body> 
       </html> 

页面名称:b.html

  <html> 
       <head> </head> 
       <body> 
        <h4>Hello</h4> 
        <div id="target"></div> 
       </body> 
      </html> 
+0

如果您的帖子的代码一面墙,如果你把它的赞赏一个[jsfiddle.net ](http://jsfiddle.net)链接! –

+0

什么浏览器?为我工作很好:http://jsfiddle.net/pPdwC/ –

回答

1

通过使用document.write在页面加载后,要更换的文件。

窗口

使用load事件,这样一旦加载,您可以访问该文件的内容:

$(newWind).load(function() { 
    $('#target', newWind.document).html(printableHTML); 
}); 
+0

感谢它工作正常 – Hearaman

0

而不是弹出一个新窗口,用您想要打印的位填充它,更好的解决方案是将其打印为标准window.print()并使用打印介质样式表来显示/隐藏要打印的页面部分。

@media print { 
    /* style sheet for print goes here */ 
} 
0

变化

var newWind=window.open('b.html', 'Print message', ... 

window.open('b.html', 'Print_message', ... 

然后,你可以做

Print_message.document.write(printableHTML);