你不應該更換整個頁面的HTML,因爲這會刪除所有點擊處理程序(用於處理例如標籤的那些)
要打印,你應該做到以下幾點:
- 創建一個高於一切顯示一個div(固定的,高度/寬度:100%,頂端/左:0)
- 隱藏在主體
- 一切添加所述內容將被打印到第Ë格
- 呼叫打印
- 刪除DIV
- 恢復身體狀態
喜歡的東西:
JS
function printReport() {
var $printerDiv = $('<div class="printContainer"></div>'); // create the div that will contain the stuff to be printed
$printerDiv.html(divElements); // add the content to be printed
$('body').append($printerDiv).addClass("printingContent"); // add the div to body, and make the body aware of printing (we apply a set of css styles to the body to hide its contents)
window.print(); // call print
$printerDiv.remove(); // remove the div
$('body').removeClass("printingContent");
}
CSS
body.printingContent > *{
display: none !important; /* hide everything in body when in print mode*/
}
.printContainer {
display: block !important; /* Override the rule above to only show the printables*/
position: fixed;
z-index: 99999;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
你能更詳細地瞭解'window.print();'後面會發生什麼嗎?您無法點擊「其他標籤」 - 是因爲網頁上的以前的內容沒有顯示,或者因爲它顯示,但不是不響應點擊? – ASGM 2013-05-10 08:23:12
如果我關閉打印窗口或點擊打印選項,然後打印窗口將關閉並顯示我的頁面,但之後在我的頁面我無法點擊任何標籤或按鈕等 – Prashobh 2013-05-10 08:28:04