网站首页 文章专栏 js 前端生成二维码,并可以一键下载
引入一个插件 http://static.hebinghua.com/plugin/qrcode/qrcode.js
<body> <label class="control-label">页面二维码:</label> <span onclick="downloadQrcode()">点击下载</span> <div id="wx_code"> </div> </body> <script> var qrcode = new QRCode(document.getElementById("wx_code"), { text: "hahaha", //你想要填写的文本 width: 100, //生成的二维码的宽度 height: 100, //生成的二维码的高度 colorDark : "#000000", // 生成的二维码的深色部分 colorLight : "#ffffff", //生成二维码的浅色部分 correctLevel : QRCode.CorrectLevel.H }); function downloadQrcode(){ var qrcode = document.getElementById('wx_code'); var img = qrcode.getElementsByTagName('img')[0]; var link = document.createElement("a"); var url = img.getAttribute("src"); link.setAttribute("href",url); link.setAttribute("download",'123.png'); link.click(); } </script>
转载请注明出处