js打印的另一种实现方式(可以打印图片,二维码等)

//idstr--打印内容的id
function printView(idstr){
    var el = document.getElementById(idstr);
            var iframe = document.createElement('IFRAME');
            var doc = null;
            iframe.setAttribute('style',             
            'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
            document.body.appendChild(iframe);
            doc = iframe.contentWindow.document;
            doc.write('
' + el.innerHTML + '
'); doc.close(); var img = $(doc).find("img"); for(var i = 0; i < img.length; i++) { img.eq(i).css("margin", "0 auto"); } iframe.contentWindow.focus(); iframe.contentWindow.print(); if(navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe); } }

 

你可能感兴趣的:(JavaScript,js打印,打印图片,打印二维码)