我只想打印QR码div而不是整页。谁能帮我。

这是我的代码:

<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>

提前致谢。

最佳答案

我遇到了同样的问题,遇到了这个解决方案

<!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>
学分:阿司匹林

10-08 16:55