当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 基于jquery二维码生成插件qrcode

基于jquery二维码生成插件qrcode

2019年03月24日  | 移动技术网IT编程  | 我要评论
1、首先在页面中加入jquery库文件和qrcode插件。 ? 1 2 <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js" ...

1、首先在页面中加入jquery库文件和

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

2、在页面布局中添加一个div

?
1
2
3
<div class="modal-body" id="qrcode" style="left:40px">
  
 </div>

3、调用qrcode插件。

?
1
2
3
4
5
6
var str = "http://" + location.host + "/activitydetail.html?id=" + row.activityguid + "&ismail=" + row.ismail + "";
$("#qrcode").empty();
  
$('#qrcode').qrcode(str);
  
//$('#qrcode').qrcode("//www.jb51.net");//任意字符串

4、我们试验的时候发现不能识别中文内容的二维码,通过查找多方资料了解到,jquery-qrcode是采用charcodeat()方式进行编码转换的。而这个方法默认会获取它的unicode编码,如果有中文内容,在生成二维码前就要把字符串转换成utf-8,然后再。您可以通过以下函数来转换中文字符串:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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; 
}

可以把这个方法直接写入到引用的插件里面,后面直接调用即可。如下:

?
1
2
var str = toutf8("2017鸡年大吉!");
$('#qrcode').qrcode(str);

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网