js的window.print()打印背景圖片
題目描述
js的window妆丘,print()打印背景圖片給body加了圖片地址之后,非要設(shè)置瀏覽器打印選項里面設(shè)置背景圖形打印才行浇坐,怎么通過js去設(shè)置默認的就是打印帶背景圖像的
方法一 :css
//打印機媒體查詢
@media print {
body{
-webkit-print-color-adjust:exact;
-moz-print-color-adjust:exact;
-ms-print-color-adjust:exact;
print-color-adjust:exact;
}
button{display:none;}
}
方法二 :js
function printPage(){
var office = document.getElementsByClassName('office')[0];
var imgs = office.getElementsByTagName('img');
var img,src;
if(!imgs.length){
src = window.getComputedStyle(office,null).getPropertyValue('background-image').replace(/(^url)|\(|\)|\"/g,'');
img = document.createElement('img');
img.src=src;
img.height = 150;
img.width = 150;
office.style.backgroundImage = 'none';
office.appendChild(img);
}
window.print();
//取消打印還原
setTimeout(function(){
office.removeChild(img);
office.style.backgroundImage = 'url("'+ src +'")';
},100)
}