zoukankan      html  css  js  c++  java
  • 实现邀请码的复制粘贴

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

  • 相关阅读:
    angular2
    angular1
    JavaScript随笔1
    鼠标样式
    清除浮动
    css-父标签中的子标签默认位置
    [Leetcode] Decode Ways
    [Java] 利用LinkedHashMap来实现LRU Cache
    LinkedHashMap和HashMap的比较使用(转)
    [Java] java.util.Arrays 中使用的 sort 采用的算法 (转)
  • 原文地址:https://www.cnblogs.com/redFeather/p/15217505.html
Copyright © 2011-2022 走看看