实现邀请码的复制粘贴
一、点击input框进行复制
<input type="text" readonly="readonly" onchange="this.defaultValue=this.value" value="" class="input" id="text_input" onClick="copy($('.input').val())"/> function copy(message) { var input = document.createElement("input"); input.value = message; document.body.appendChild(input); input.select(); input.setSelectionRange(0, input.value.length), document.execCommand('Copy'); document.body.removeChild(input); //alert("复制成功"); }
二、点击按钮复制文本
<div class="hema_code"><span id="content">邀请码</span></div> <div class="heme_copy" id="copyBT">复制</div>
function copyArticle(event) {
const range = document.createRange();
range.selectNode(document.getElementById('content'));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
var content=$("#content").html();
if(content != "" && content != null && content != undefined){
alert("复制成功!");
}
}
document.getElementById('copyBT').addEventListener('click', copyArticle, false);