实现邀请码的复制粘贴


一、点击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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM