QRcodeJS產生二維碼,qrcodejs產生

來源:互聯網
上載者:User

QRcodeJS產生二維碼,qrcodejs產生
QRCode.js

QRCode.js是依賴JS產生二維碼的.主要是通過擷取DOM的標籤,再通過HTML5Canvas繪製而成,不依賴JQ

擷取QRCode.js
  • Github-Page:qrcode.js
    • 有tar.gz包和zip包.
  • 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.