使用 JavaScript 生成二维码

QRCode.js 基本用法

<div id="qrcode"></div> <script type="text/javascript"> new QRCode(document.getElementById("qrcode"), "http://www.runoob.com"); // 设置要生成二维码的链接 </script>

或者使用一些可选参数设置:

var qrcode = new QRCode("test", { text: "http://www.runoob.com", width: 128, height: 128, colorDark : "#000000", colorLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H });

同样我们可以使用以下方法:

qrcode.clear(); // 清除代码 qrcode.makeCode("http://www.w3cschool.cc"); // 生成另外一个二维码

 

实例代码

HTML 代码

<input id="text" type="text" value="http://www.runoob.com" /><br /> <div id="qrcode"></div>

CSS 代码

#qrcode {
width:160px; height:160px; margin-top:15px; }

JavaScript 代码

var qrcode = new QRCode("qrcode"); 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(); } });

猜你喜欢

转载自www.cnblogs.com/zxz101/p/11232827.html