需求:根据url地址生成二维码,微信扫描二维码可直接打开网址
html代码:
<input id="text" type="text" value="http://www.runoob.com"
onblur="makeCode();" onkeydown="keyEnter();"/>
<div id="qrcodeContent">
<div id="qrcode"></div>
</div>
css代码:
input{
width:200px;
height:40px;
line-height:40px;
padding:0 10px;
border:1px solid #e7e7e7;
border-radius:5px;
}
#qrcodeContent{
padding:10px;
}
#qrcode{
width:200px;
height:200px;
}
JS代码:
<script src="build/qrcode.min.js"></script>
<script>
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 200,
height : 200
});
function makeCode(){
var elText = document.getElementById("text");
if (!elText.value) {
alert("Input a text");
elText.focus();
return;
}
qrcode.makeCode(elText.value);
}
function keyEnter() {
if(event.keyCode==13){
makeCode();
}
}
makeCode();
学习地址:https://www.runoob.com/w3cnote/javascript-qrcodejs-library.html
资源下载
Qrcode 库及实例下载:qrcodejs-04f46c6.zip
Github 地址:https://github.com/davidshimjs/qrcodejs