javascript二維碼的生成可以用第三方庫qrcode.js。
下載地址:https://gitcode.net/mirrors/davidshimjs/qrcodejs
解壓后打開index.html文件輸入百度地址回車,就可以看到指定頁面的二維碼了。
?
?html代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<title>Cross-Browser QRCode generator for Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="qrcode.js"></script>
</head>
<body>
<input id="text" type="text" value="http://jindo.dev.naver.com/collie" style="width:80%" /><br />
<div id="qrcode" style="width:100px; height:100px; margin-top:15px;"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 100,
height : 100
});
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();
}
});
</script>
</body>
根據(jù)使用文檔,二維碼的生成需要QRCode對象。?
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: "http://jindo.dev.naver.com/collie",
width: 128,
height: 128,
colorDark : "blue",
colorLight : "red",
correctLevel : QRCode.CorrectLevel.H
});
QRCode有兩個參數(shù),前一個是插入二維碼DOM,后一個可以設(shè)置二維碼的屬性。文章來源:http://www.zghlxwxcb.cn/news/detail-520980.html
text//網(wǎng)址
width//寬度
height//高度
colorDark//點(diǎn)顏色
colorLight//背景顏色
correctLevel//容錯率
//correctLevel : QRErrorCorrectLevel.L,(7%)
//correctLevel : QRErrorCorrectLevel.M,(15%)
//correctLevel : QRErrorCorrectLevel.Q,(25%)
//correctLevel : QRErrorCorrectLevel.H,(30%)
colorDark : "blue",
colorDark : "blue",
colorLight : "red",
?QRCode有兩個方法:文章來源地址http://www.zghlxwxcb.cn/news/detail-520980.html
qrcode.clear(); // 清除二維碼
qrcode.makeCode("http://naver.com"); // 生成其它二維碼
到了這里,關(guān)于【javascript】二維碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!