jQuery QRCode

前端   2025-02-18 15:09   67   0  

首先,你需要在HTML文件中引入jQuery库以及jquery.qrcode.min.js插件文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>jQuery QRCode 示例</title>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.qrcode.min.js"></script>
</head>
<body>
    <!-- 用于放置二维码的div -->
    <div id="qrcode"></div>
 
    <script>
        // 生成二维码
        $(function() {
            $('#qrcode').qrcode({
                text: '这是你的二维码内容'
            });
        });
    </script>
</body>
</html>


示例:结合图片居中

如果你希望在二维码中央显示一张图片,你可以这样实现:

<div id="qrcode-container">
    <img id="qrcode-image" src="your-image-url.png" />
</div>
 
<script>
$(function() {
    var qrWidth = 128;
    var qrHeight = 128;
    $('#qrcode-image').qrcode({
        width: qrWidth,
        height: qrHeight,
        text: '你的二维码数据'
    });
 
    var imgWidth = $('#qrcode-image').width();
    var imgHeight = $('#qrcode-image').height();
 
    $('#qrcode-image').css({
        'margin-left': -(imgWidth / 2),
        'margin-top': -(imgHeight / 2)
    });
});
</script>