html
<script src="https://cdn.staticfile.org/qrcodejs/1.0.0/qrcode.min.js"></script>
<textarea placeholder="文本框" id="kw"></textarea>
<button onclick="ewm()">生成二维码</button>
<div id="erwm">
<div id="qrcode"></div>
<div id="buttons"></div>
</div>
JavaScript
function ewm() {
var kw = document.getElementById("kw").value;
var qrcodeDiv = document.getElementById("qrcode");
var buttonsDiv = document.getElementById("buttons");
if (kw.trim() === "") {
alert("请在“内容框”输入内容,文字,链接等");
return;
}else if (/[-\u4E00-\u9FA5]/.test(kw) && kw.indexOf('-') !== -1) {
var wwww = encodeURIComponent(kw);
} else {
var wwww = kw;
}
qrcodeDiv.innerHTML = "";
buttonsDiv.innerHTML = "";
document.getElementById('erwm').style.display = 'block';
var qrcode = new QRCode(qrcodeDiv, {
text: wwww,
width: 400,
height: 400
});
var downloadBtn = document.createElement("a");
downloadBtn.href = qrcodeDiv.firstChild.toDataURL();
downloadBtn.download = "二维码.png";
downloadBtn.innerHTML = "下载二维码";
downloadBtn.id = "xiazai";
var closeBtn = document.createElement("a");
closeBtn.innerHTML = "关闭二维码";
closeBtn.id = "guangb";
closeBtn.onclick = function() {
qrcodeDiv.innerHTML = "";
buttonsDiv.innerHTML = "";
document.getElementById('erwm').style.display = 'none';
};
buttonsDiv.appendChild(downloadBtn);
buttonsDiv.appendChild(closeBtn);
};