插件 <script src="${ctxPath}/libs/jquery.qrcode.min.js"></script>
<div id="qrcode"> <img src="" id="qrcodeImg"> </div>
$(function () { var qrcode=jQuery('#qrcode').qrcode({ render: "canvas", text: $('input#codeInfo').val() }); var canvas=qrcode.find('canvas').get(0); qrcode.find('canvas').remove(); $('#qrcodeImg').attr('src',canvas.toDataURL('image/jpg')); })
参考代码 <script type="text/javascript"> var qrcode=$("#code").qrcode({ render:'canvas||table', 200, //宽度 height:200, //高度 text: "http://www.baidu.com" //任意内容 }).hide(); var canvas=qrcode.find('canvas').get(0); //如果有循环,此句必不可少 qrcode.find('canvas').remove(); var data = canvas.toDataURL('image/jpg'); $('#img').attr('src',data) ; saveFile(data,"fd范德萨范德萨"); //转为图片的方法,需要上门的data function saveFile(data,filename){ var save_link=document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); save_link.href=data; save_link.download=filename; var event=document.createEvent('MouseEvents'); event.initMouseEvent('click',true,false,window,0,0,0,0,0,false,false,false,false,0,null); save_link.dispatchEvent(event); }; </script>