【前端】利用qrcode.js生成二維碼

總體代碼示例:javascript

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<title>二維碼生成</title>
	<link href="${base}/favicon.ico" rel="icon">
	<!--[if lt IE 9]>
		<script src="js/html5shiv.js"></script>
		<script src="js/respond.js"></script>
	<![endif]-->
	<script src="js/jquery.js"></script>
	<script src="js/jquery.qrcode.js"></script>
	
	<style>
	</style>

			<script>
			$().ready(function() {
				
				// 二維碼
				$qrcode.qrcode({
					width: 300,
					height: 300,
					text: "www.20eit.cn"
				});
				
			});
			
			</script>
</head>
<body>
<div class="container">
		
	<div class="wx-qr-box" id="qrcode" class="qrcode"></div>
			
</div>
</body>
</html>

總體代碼及案例:php

https://gitee.com/20eit/eit/tree/master/qrcode/html

更多使用案例引用:https://www.runoob.com/w3cnote/javascript-qrcodejs-library.htmlhtml5

QRCode.js:使用 JavaScript 生成二維碼

分類 編程技術

什麼是 QRCode.js?

QRCode.js 是一個用於生成二維碼的 JavaScript 庫。主要是經過獲取 DOM 的標籤,再經過 HTML5 Canvas 繪製而成,不依賴任何庫。java


基本用法

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

或者使用一些可選參數設置:jquery

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

一樣咱們能夠使用如下方法:git

qrcode.clear(); // 清除代碼
qrcode.makeCode("http://www.w3cschool.cc"); // 生成另一個二維碼

瀏覽器支持

支持該庫的瀏覽器有:IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile, 等。github


實例代碼

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();
    }
});

嘗試一下 »編程


資源下載

Qrcode 庫及實例下載:qrcodejs-04f46c6.zip瀏覽器

Github 地址:https://github.com/davidshimjs/qrcodejs

相關文章
相關標籤/搜索