2017-07-18 69 views
1

我想僅打印QR碼div而不是整頁。誰能幫我。如何使用jquery/javascript打印QR碼

這裏是我的代碼:

<div id="qrcodeCanvas"></div> //Generating QR in this div 

<a id="Html2Image" href="#qrcodeCanvas">Download</a> 
<a id="mydiv" href="javascript:void(0);" onClick="PrintDiv();" >Print</a> 
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js' type='text/javascript'></script> 
<script type="text/javascript" src="js/jquery.qrcode.js"></script> 
<script type="text/javascript" src="js/qrcode.js"></script> 


<script> 
function PrintDiv() 
{ 

} 
</script> 


<script> 
var id = 'Content'; 
$('#qrcodeCanvas').qrcode(id); 

var canvas = $('#qrcodeCanvas canvas'); 
var img = $(canvas)[0].toDataURL("image/png"); 

$("#Html2Image").attr("download", "QR_Code.png").attr("href", img); //Downloading QR image 
</script> 

在此先感謝。

+0

「打印」 你的意思是實際_printing_:

相反,認爲這無損的方法呢?如果是這樣,使用'window,print()'並隱藏除CSS之外的所有內容。 – evolutionxbox

回答

0

我有同樣的問題,來到accros這sollution

<!DOCTYPE html> 
<html> 
<body> 

    <div id="printableArea"> 
    <div id="qrcodeCanvas"></div> 
</div> 
<script> 
function printDiv(divName) { 
var printContents = document.getElementById(divName).innerHTML; 
var originalContents = document.body.innerHTML; 

document.body.innerHTML = printContents; 

window.print(); 

document.body.innerHTML = originalContents; 
} 
</script> 
<input type="button" onclick="printDiv('printableArea')" value="print a qr!" /> 

</body> 
</html> 

學分:阿司匹林

+0

不打印QR碼,只打印div數據 –

+0

@VkyArain它確實打印QR碼注意在線5上的

! – ImmanuelNL

+0

不打印,請自己測試 –

0

@ ImmanuelNL的答案應該工作,但它會用核武器炸一切。事件處理程序,任何對代碼中元素的引用,一切都將被打破。當你說

var tmp = document.createDocumentFragment(), 
    printme = document.getElementById('printableArea').cloneNode(true); 
while(document.body.firstChild) { 
    // move elements into the temporary space 
    tmp.appendChild(document.body.firstChild); 
} 
// put the cloned printable thing back, and print 
document.body.appendChild(printme); 
window.print(); 

while(document.body.firstChild) { 
    // empty the body again (remove the clone) 
    document.body.removeChild(document.body.firstChild); 
} 
// re-add the temporary fragment back into the page, restoring initial state 
document.body.appendChild(tmp);