码迷,mamicode.com
首页 > Web开发 > 详细

jquery-qrcode

时间:2017-07-26 15:35:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:二维   string   func   内容   att   for   生成   中文字符   中文   

jquery -qrcode

github地址https://github.com/jeromeetienne/jquery-qrcode

copy jquery.qrcode.min.js和jquery.mini.js到项目中


加入div

<div id="qrcode"></div>

 

生成二维码

<script type="text/javascript">
  $(‘#qrcode‘).qrcode(toUtf8("钓鱼岛是中国的!"));
    $(‘#qrcode‘).qrcode({
    width : 64,
    height : 64,
    text : "size doesn‘t matter"
  });
</script>

 

如果二维码是中文,存在乱码问题
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;
    }

 

jquery-qrcode

标签:二维   string   func   内容   att   for   生成   中文字符   中文   

原文地址:http://www.cnblogs.com/miye/p/7239780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!