js二维码的生成方法

一、js生成二维码有很多方法这里只写个人制作的一种

二、效果图

三、直接上代码

<!DOCTYPE html>
<html>
<head>
<title>Javascript 二维码生成库:QRCode</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="https://cdn.staticfile.net/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js"></script>
</head>
<body>
<input id="text" type="text" value="https://www.runoob.com" 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>
</html>

希望可以对大家有所帮助;

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用第三方库如qrcode.js生成二维码,并通过JavaScript生成二维码添加到弹窗中。以下是一个示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>生成二维码并显示在弹窗中</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script> <style> #popup { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; height: 400px; background-color: #fff; border: 1px solid #ccc; padding: 20px; } </style> </head> <body> <button id="show-popup">显示二维码</button> <div id="popup"> <div id="qrcode"></div> </div> <script> $(function() { $("#show-popup").on("click", function() { // 生成二维码 var qrcode = new QRCode(document.getElementById("qrcode"), { text: "https://www.baidu.com", width: 300, height: 300, colorDark: "#000", colorLight: "#fff", correctLevel: QRCode.CorrectLevel.H }); // 显示弹窗 $("#popup").show(); }); }); </script> </body> </html> ``` 在上面的示例中,我们通过点击按钮来触发生成二维码并显示在弹窗中的操作。在JavaScript中,我们使用了jQuery库来方便地操作DOM元素,使用了qrcode.js库来生成二维码。其中,`QRCode`函数可以接收一个包含二维码生成选项的对象作为参数,例如`text`表示二维码中的文本内容,`width`和`height`表示二维码的宽度和高度,`colorDark`和`colorLight`表示二维码的颜色等。最后,我们通过设置弹窗的CSS来使其位于页面中心,并在JavaScript中使用`show()`函数将其显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值