2010-09-16 101 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

相關問題