2010-10-01 94 views
40

我有一個帶有DIV元素的網頁。當用戶點擊「打印」時,我想打印該div的內容。請注意,我只想打印DIV的內容,而不是整個頁面。爲了嘗試這個,我決定使用JavaScript打開一個新窗口。然後我將把DIV的內容寫入新窗口。我的問題是,這可能與JQuery?如果是這樣,怎麼樣?目前,我嘗試以下:用JQuery將內容寫入新窗口

function printClick() { 
    var w = window.open(); 
    var html = $("#divToPrintID").html(); 

    // how do I write the html to the new window with JQuery? 
} 
+0

這裏有一個類似的線程:http://stackoverflow.com/questions/1225558/jquery-new-window-with-content-from-modal- content-on-opener-page – orolo 2010-10-01 16:29:17

回答

4

文件1:

function find(input){ 
    return $(input); 
} 

function printClick() { 
    var w = window.open(); 
    var html = $("#divToPrintID").html(); 

    // how do I write the html to the new window with JQuery? 
} 

,並在第二個文件做到這一點:

var html = window.opener.find("#divToPrintID").html(); 
52

您可以使用

$(w.document.body).html(html); 

this example

+0

這似乎不適用於我的IE7。任何已知的問題呢? – wonza 2013-01-09 14:34:45