function ADDqrcode(str) {
// 创建关闭按钮并设置样式
var $closeBtn = $('<button>X</button>').css({
'position': 'absolute',
'top': '5px',
'right': '5px',
'font-size': '16px',
'line-height': '20px',
'cursor': 'pointer',
'background-color': 'rgb(169 169 169)',
'border': '1px solid rgb(204, 204, 204)',
'color': 'aliceblue',
});
// 关闭按钮事件
$closeBtn.on('click', function() {
$qrDiv.remove();
});
// 创建二维码元素并设置样式
var $qrDiv = $('<div id="getqrCode"/>').css({
'position': 'fixed',
'top': '50%',
'left': '50%',
'transform': 'translate(-50%, -50%)',
'background-color': 'white',
'border': '1px solid #c5c5c5',
'padding': '20px',
'z-index': '9999',
'border-radius': '10px',
});
// 将关闭按钮添加到二维码元素
$qrDiv.append($closeBtn);
// 创建输入框和确定按钮并添加到二维码元素中
var $inputWrapper = $('<div/>').css({
'width': '100%',
'margin-top': '10px'
}).appendTo($qrDiv);
// 创建输入框并设置样式
var $input = $('<input type="text" placeholder="请输入需要生成的内容"/>').css({
'width': 'calc(100% - 80px)',
'padding': '5px',
'font-size': '16px',
'border': '1px solid #ccc',
'border-radius': '3px'
}).appendTo($inputWrapper);
// 创建确定按钮并设置样式
var $confirmBtn = $('<button>确定</button>').css({
'display': 'inline-block',
'padding': '5px 10px',
'margin-left': '10px',
'font-size': '16px',
'text-align': 'center',
'color': '#fff',
'background-color': '#42b983',
'border': 'none',
'border-radius': '3px',
'cursor': 'pointer'
}).on('click', function() {
var text = $input.val();
if (text) {
// 对输入文本进行编码转换
text = toUtf8(text);
// 生成自定义的二维码并替换原有的二维码
$canvas.find('canvas').remove();
$canvas.qrcode({
'width': 200,
'height': 200,
'text': text
});
} else {
alert('请输入需要生成的内容!');
}
}).appendTo($inputWrapper);
// 创建包裹二维码的 canvas 容器
var $canvas = $('<div/>').css({
'margin-top': '20px',
'text-align': 'center'
}).appendTo($qrDiv);
// 添加二维码元素到 body 中
$('body').append($qrDiv);
// 生成二维码
$canvas.qrcode({
'width': 200,
'height': 200,
'text': window.location.href
});
$canvas.after('<p style="padding: 10px" >首次打开默认的二维码为当前页面的网址</p>')
};
function toUtf8(str) {
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}