2010-09-16 100 views
0

我的网页上有两个部分,SectionA和SectionB(2个HTML表格)。如何在javascript中打印页面的两个部分之一

我希望能够打印整个页面,只有SectionA或只有SectionB。

我已经有一个CSS文件media="print",我用window.print()

要打印整个页面,请使用浏览器打印按钮/菜单项。

只打印部1.:

function PrintSectionA() 
{ 
    $('#SectionA').removeClass('hideFromPrint'); 
    $('#SectionB').addClass('hideFromPrint'); 
    window.print(); 
} 

和相对来说PrintSectionB() 呀!它工作...差不多。如果我尝试只打印SectionA,,然后整个页面,我只会得到SectionA,因为SectionB仍然有hideFromPrint类。

我会希望是:将文档发送到打印机

function PrintSectionA() 
{ 
    $('#SectionA').removeClass('hideFromPrint'); 
    $('#SectionB').addClass('hideFromPrint'); 
    window.print(); 
    $('#SectionB').removeClass('hideFromPrint'); 
} 

window.print();返回之前。所以PrintSectionA()实际打印现在一切:(。

有没有一种方法,使工作?

我想我看见的地方,我可以强制CSS分页,我可以要求用户打印整个页面,但只能选择第一或第二页......没有乐趣!

+0

也许更好的解决方案是创建两个CSS用于打印的文件,并且只是将链接更改为打印CSS文件吨或链接被点击。 – 2010-09-16 22:25:23

回答

1

怎么样定义三个功能(其中一个是“所有打印”)?

function PrintSectionA() 
{ 
    $('#SectionA').removeClass('hideFromPrint'); 
    $('#SectionB').addClass('hideFromPrint'); 
    window.print(); 
} 

function PrintSectionB() 
{ 
    $('#SectionB').removeClass('hideFromPrint'); 
    $('#SectionA').addClass('hideFromPrint'); 
    window.print(); 
} 

function PrintAll() 
{ 
    $('#SectionA').removeClass('hideFromPrint'); 
    $('#SectionB').removeClass('hideFromPrint'); 
    window.print(); 
} 
+0

谢谢,但我没有“打印所有”按钮在我的网页上。 PrintAll函数需要在打印之前由浏览器调用(使用File> Print菜单选项),并且我认为onPrint没有事件。我会和老板一起看,如果我可以添加一个打印所有按钮...... – Guillaume 2010-09-17 13:01:53

相关问题