QRCode.js是一個生成二維碼的JS庫。QRCode.js支持在DOM中使用跨瀏覽器Canvas和table標籤的。 QRCode.js不依賴其餘JS庫。
javascript
基本用法:html
<div id="qrcode"></div> <script type="text/javascript"> new QRCode(document.getElementById("qrcode"), "http://jindo.dev.naver.com/collie"); </script>
還能夠添加其它選項:java
<div id="qrcode"></div> <script type="text/javascript"> var qrcode = new QRCode(document.getElementById("qrcode"), { text: "http://jindo.dev.naver.com/collie", width: 128, height: 128, colorDark : "#000000", colorLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H }); </script>
也可使用一些方法:
jquery
qrcode.clear(); // 清除二維碼 qrcode.makeCode("http://naver.com"); // 生成一個新的二維碼
瀏覽器兼容性:git
IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile 等等github
案例演示:瀏覽器
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" /> <title></title> </head> <body> <input id="text" type="text" value="http://dapengtalk.blog.51cto.com" style="width:80%" /><br /> <div id="qrcode" style="width:100px; height:100px; margin-top:15px;"></div> <script src="js/jquery-1.8.3.min.js"></script> <script src="js/qrcode.js"></script> <script> var qrcode = new QRCode(document.getElementById("qrcode"), { width : 100, height : 100 }); function makeCode () { var elText = document.getElementById("text"); if (!elText.value) { alert("Input a text"); elText.focus(); return; } qrcode.makeCode(elText.value); } makeCode(); $("#text"). on("blur", function () { makeCode(); }). on("keydown", function (e) { if (e.keyCode == 13) { makeCode(); } }); </script> </body> </html>
頁面截圖:ide
掃描二維碼:ui
Github 地址:https://github.com/davidshimjs/qrcodejs spa