QRcodeJS產生二維碼,qrcodejs產生
QRCode.js
QRCode.js是依賴JS產生二維碼的.主要是通過擷取DOM的標籤,再通過HTML5Canvas繪製而成,不依賴JQ
擷取QRCode.js
- Github-Page:qrcode.js
- Github-Repository
引入及用法引入
- 只要在
<head></head>中引入即可使用,JQ不依賴,可以替換其他版本JQ(其他內容有用到JQ,比如選取器擷取)
<script type="text/javascript" src="qrcode.js"></script>
基本用法
擷取塊,直接調用預設方法(輸入產生的字串即可產生二維碼)
<div id="qrcode"></div><script type="text/javascript"> new QRCode(document.getElementById("qrcode"), "https//blog.csdn.net/crper");</script>
進階用法
QRCODE支援以下參數:
- width(寬度)
- height(高度)
- colorDark(背景色)
- colorLight(前景色彩)
- correctLevel(容錯層級,支援L,M,H)Low/Middle/High
var qrcode = new QRCode("test", { text: "https//blog.csdn.net/crper", width: 400, height: 400, colorDark : "#000000", colorLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H});qrcode.clear(); // 清除二維碼qrcode.makeCode("http://naver.com"); // 產生另外一個二維碼
線上產生函數
這個函數是擷取id為text,判斷內容是否為空白,為空白則彈窗提醒,不為空白則產生二維碼
function makeCode () { var elText = document.getElementById("text"); if (!elText.value) { alert("請輸入您要產生的二維碼內容!"); elText.focus(); return; } qrcode.makeCode(elText.value);}makeCode();
通過調用blur和keydown來觸發,,當脫離焦點點擊的時候產生二維碼,或者輸入內容後按下斷行符號鍵(Enter)產生
$("#text"). on("blur", function () { makeCode(); }). on("keydown", function (e) { if (e.keyCode == 13) { makeCode(); } });
實戰小例子
<!DOCTYPE html><html> <head> <title>Demo</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" /> <!--引用本地指令檔--> <script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="qrcode.js"></script> <style> #qrcode { height: 300px; width: 300px; background: #eee; } input { height: 25px; line-height: 25px; width: 300px; } </style> </head> <body> <input id="text" type="text" value="這是一個測試文本,清除後輸入您要產生的內容" /> <div id="qrcode"></div> <!--指令碼就是要放在後面.......--> <script type="text/javascript"> var qrcode = new QRCode(document.getElementById("qrcode"), { width: 300, height: 300, colorDark: "#000000", colorLight: "#03f594", correctLevel: QRCode.CorrectLevel.L }); 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>
瀏覽器適配,支援下列
| PC |
Mobile |
| IE6~10 |
Mobile Safari |
| Chrome |
Android |
| Firefox |
Windows Mobile |
| Safari |
|
| Opera |
|
遵循協議
MIT License