本文将介绍如何使用JS生成二维码,兼容各种浏览器及手机端,支持中文。
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title> JS 生成二维码 </title>
<script src="qrcode.js"></script>
<script type="text/javascript">
window.onload = function(){
var qrcode;
var content;
var size;
document.getElementById("send").onclick =function(){
content = document.getElementById("content").value;
content = content.replace(/(^\s*)|(\s*$)/g, "");
size = document.getElementById("size").value;
if(content==''){
alert('请输入内容!');
return false;
}
if(!/^[0-9]*[1-9][0-9]*$/.test(size)){
alert('请输入正整数');
return false;
}
if(size<100 || size>500){
alert('尺寸范围在100~500');
return false;
}
if(qrcode){
qrcode.clear();
}
qrcode = new QRCode(document.getElementById("qrcode"), {
width : size,
height : size
});
qrcode.makeCode(document.getElementById("content").value);
}
}
</script>
</head>
<body>
<p>内容:<input type="text" id="content" value="http://weibo.com/fdipzone" /></p>
<p>尺寸:<input type="text" id="size" value="150"></p>
<p><button id="send">生成二维码</button></p>
<div id="qrcode"></div>
</body>
</html>
qrcode.js文件及完整代码下载地址:点击查看
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)