尝试打开 new window
, append pre
html
, style
,如果有的话pre
,到 new window
document.body
, call .focus()
,.print()
on new window
;选择系统打印对话框,选择“打印到文件”
$("#save").click(function() {
var text = $("#output")[0].outerHTML;
// `styles`: `style` element;
// or `String` "<style>.light{color:#0af;}</style>" ;
// alternatively , add `style` attribute to `pre` element directly,
// e.g., `<pre style="color:#0af;">`
var styles = $("style")[0].outerHTML;
var popup = window.open("", "popup");
// append `text`:`pre` element `styles`:`style` element
// at `popup` `document.body`
popup.document.body.innerHTML = text + styles;
popup.focus();
popup.print();
});
jsfiddle http://jsfiddle.net/tn04Ldka/2/