產生二維碼,二維碼的產生

來源:互聯網
上載者:User

標籤:from   二維   http   gui   檔案   rom   sha   har   sharp   

本文將介紹一款基於jquery的二維碼產生外掛程式qrcode,在頁面中調用該外掛程式就能產生對應的二維碼。

 

1、首先在頁面中加入jquery庫檔案和qrcode外掛程式。

<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js"></script> 

2、在頁面配置中添加一個div

   <div class="modal-body" id="qrCode" style="left:40px">    </div>

3、調用qrcode外掛程式。

var str = "http://" + location.host + "/ActivityDetail.html?id=" + row.ActivityGuid + "&isMail=" + row.isMail + "";$("#qrCode").empty();$(‘#qrCode‘).qrcode(str);//$(‘#qrCode‘).qrcode("http://www.helloweba.com");//任一字元串 

4、我們實驗的時候發現不能識別中文內容的二維碼,通過尋找多方資料瞭解到,jquery-qrcode是採用charCodeAt()方式進行編碼轉換的。而這個方法預設會擷取它的Unicode編碼,如果有中文內容,在產生二維碼前就要把字串轉換成UTF-8,然後再產生二維碼。您可以通過以下函數來轉換中文字串:

function toUtf8(str) {        var out, i, len, c;        out = "";        len = str.length;        for(i = 0; i < len; i++) {            c = str.charCodeAt(i);            if ((c >= 0x0001) && (c <= 0x007F)) {                out += str.charAt(i);            } else if (c > 0x07FF) {                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));                out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));            } else {                out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));            }        }        return out;    } 

可以把這個方法直接寫入到引用的外掛程式裡面,後面直接調用即可。如下:

var str = toUtf8("2017雞年大吉!"); $(‘#qrCode‘).qrcode(str); 

  

產生二維碼,二維碼的產生

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.