目前我正在試圖打印頁面上的元素的內容,我發現以下資源,什麼是用於在頁面上打印元素內容的跨瀏覽器解決方案?
http://www.808.dk/?code-javascript-print
http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/108117
http://vikku.info/codesnippets/javascript/print-div-content-print-only-the-content-of-an-html-element-and-not-the-whole-document/
他們都在做某種東西我已經有這就像以下變化,
function printelement() {
var parentdiv = $(this).parent().parent();
var iframeEle = $(document.createElement("iframe")).attr("id", "print" + formParams.fpid).css({ width: "2250px", display: "none" }).appendTo("body");
var doc = document.getElementById("print" + formParams.fpid).contentWindow.document;
doc.open();
//get stylesheets
$("link[type='text/css']").each(function() {
doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
});
//writes all the contents of div to new iframe
$(parentdiv).each(function() {
doc.write($(this).html());
});
//print the iframe
var giframe = parentdiv.contents().find("iframe");
var printdiv2 = giframe.prevObject[0].id;
if(printdiv2) {
if($.browser.msie) {
var iedoc = giframe.prevObject[0].contentWindow.document;
iedoc.execCommand('print', false, null);
}
else { giframe.prevObject[0].contentWindow.print(); }
}
else {
if($.browser.msie){
doc.execCommand('print', false, null);
}
else { iframeEle[0].contentWindow.print(); }
}
}
這似乎在FF和Chrome(儘管Chrome一直告訴我我無法在一定的時間內打印)中工作得很好。但是,在IE中它只是給我空白的結果。我怎樣才能讓這段代碼跨瀏覽器,或以不同的方式做呢?
你使用的是html5元素嗎?即扼流器在印刷他們,但有一個shiv ... – albert
你爲什麼不只是使用印刷媒體樣式表?不需要腳本。 – RobG
我沒有編寫這段代碼,但我現在也沒有時間將其更改爲此功能。 – Metropolis