js——生成二維碼
阿新 • • 發佈:2020-12-31
QRCode.js
是一個用於生成二維碼的 JavaScript 庫。主要是通過獲取 DOM 的標籤,再通過 HTML5 Canvas 繪製而成,不依賴任何庫。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<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="http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src= "http://static.runoob.com/assets/qrcode/qrcode.min.js"></script>
</head>
<body>
<input id="text" type="text" value="http://www.runoob.com" style="width:80%" /><br />
<div id="qrcode" style="width:100px; height:100px; margin-top:15px;" ></div>
<script type="text/javascript">
//根據輸入框輸入得地址獲取二維碼
$("#text").on("keydown", function (e) {
if (e.keyCode == 13) {
makeCode()
}
})
//製造二維碼
function makeCode() {
var elText = document.getElementById("text")
if (!elText.value) {
alert("請輸入地址")
return
}
//生成二維碼 樣式 屬性
var qrcode = new QRCode(document.getElementById("qrcode"), {
width: 100,
height: 100
})
qrcode.makeCode(elText.value)
}
</script>
</body>
</html>