找了一堆代码一堆GPT,终于给写对了:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QRCode Example</title>
<script src="https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas> <!-- Canvas 元素 -->
<script>
// 获取 canvas 元素
const canvas = document.getElementById('canvas');
// 使用 qrcode 库生成二维码
QRCode.toCanvas(canvas, 'Hello World', function (error) {
if (error) {
console.error(error);
} else {
console.log('QR code successfully generated!');
}
});
</script>
</body>
</html>