代码用旧版的printarea进行网页打印
(function (jQuery) {
var printAreaCount = 0;
jQuery.fn.printArea = function () {
var ele = jQuery(this);
var idPrefix = "printArea_";
removePrintArea(idPrefix + printAreaCount);
printAreaCount++;
var iframeId = idPrefix + printAreaCount;
var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
iframe = document.createElement('IFRAME');
jQuery(iframe).attr({
style: iframeStyle,
id: iframeId
});
document.body.appendChild(iframe);
var doc = iframe.contentWindow.document;
jQuery(document).find("link").filter(function () {
return jQuery(this).attr("rel").toLowerCase() == "stylesheet";
}).each(
function () {
doc.write('<link type="text/css" rel="stylesheet" href="'
+ jQuery(this).attr("href") + '" >');
});
// doc.write('<div class="printlogo" ><img src="'+webPath+'/resources/style/'+version+'/usercenter/images/printlogo.jpg"/> </div>');
doc.write('<div class="' + jQuery(ele).attr("class") + '">' + jQuery(ele).html()
+ '</div>');
var bdhtml= doc.body.innerHTML;
var a=bdhtml.substr(bdhtml.indexOf("<div class=\"printtitle marginb30 margint30\">"),bdhtml.indexOf("<div class=\"printtitle marginb30 margint30\">")+1000);
doc.close();
var frameWindow = iframe.contentWindow;
frameWindow.close();
frameWindow.focus();
var a = frameWindow.print();
var b="1";
}
var removePrintArea = function (id) {
jQuery("#iframe" + id).remove();
};
})
原始代码一直用着没问题,突然有的浏览器点击打印时预览出来一片空白
查看官网最新的源码发现
doc = iframe.contentWindow.document; 使用时需要先open
加上doc.open();问题解决