trying to print the contents in a new window

2019-08-27 20:22发布

问题:

I am trying to print the html contents to a new window.

var w= window.open();
var htmlContent = $(printdiv).html(printcontent);
$(w.document.body).html(htmlContent);
$(w).location.reload();
$(w).focus();
$(w).print();
$(w).close();

I am a getting a blank window with no html contents instead?

回答1:

Try setting new window name reference , removing $() wrapper around w at .focus() , .print() , .close() as jQuery() does not have method .print() calling .print() chained to $() would return TypeError: undefined is not a function

// set `w` name reference
var w = window.open("", "w");
var htmlContent = $(printdiv).html(printcontent);
$(w.document.body).html(htmlContent);
// $(w).location.reload();
w.focus();
w.print();
w.close();

jsfiddle http://jsfiddle.net/14f5owux/