實現邀請碼的復制粘貼


一、點擊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