Copy text string on click

后端 未结 8 1910
我在风中等你
我在风中等你 2020-11-27 02:31

I spent a good 20 min searching online for this, but couldn\'t find it. What I want is to to be able to copy a text string on click without a button. The text strin

相关标签:
8条回答
  • You can attach copy event to <span> element, use document.execCommand("copy") within event handler, set event.clipboardData to span .textContent with .setData() method of event.clipboardData

    const span = document.querySelector("span");
    
    span.onclick = function() {
      document.execCommand("copy");
    }
    
    span.addEventListener("copy", function(event) {
      event.preventDefault();
      if (event.clipboardData) {
        event.clipboardData.setData("text/plain", span.textContent);
        console.log(event.clipboardData.getData("text"))
      }
    });
    <span>text</span>

    0 讨论(0)
  • 2020-11-27 03:16

    function copy(that){
    var inp =document.createElement('input');
    document.body.appendChild(inp)
    inp.value =that.textContent
    inp.select();
    document.execCommand('copy',false);
    inp.remove();
    }
    <p onclick="copy(this)">hello man</p>

    0 讨论(0)
  • 2020-11-27 03:21

    This is the Code pen.

    <link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'> 
    
    <p style="color:wheat;font-size:55px;text-align:center;">How to copy a TEXT to Clipboard on a Button-Click</p>
    
    <center>
    <p id="p1">This is  TEXT 1</p>
    <p id="p2">This is TEXT 2</p><br/>
    
    <button onclick="copyToClipboard('#p1')">Copy TEXT 1</button>
    <button onclick="copyToClipboard('#p2')">Copy TEXT 2</button>
    
    <br/><br/><input class="textBox" type="text" id="" placeholder="Dont belive me?..TEST it here..;)" />
    </center>
    

    Jquery Code here

    function copyToClipboard(element) {
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val($(element).text()).select();
      document.execCommand("copy");
      $temp.remove();
    }
    
    0 讨论(0)
  • 2020-11-27 03:24

    Along with copying the text , you also have to make sure that any previously selected component remains selected after copying to clipboard.

    Here's the full code :

    const copyToClipboard = str => {
      const el = document.createElement('textarea');  // Create a <textarea> element
      el.value = str;                                 // Set its value to the string that you want copied
      el.setAttribute('readonly', '');                // Make it readonly to be tamper-proof
      el.style.position = 'absolute';                 
      el.style.left = '-9999px';                      // Move outside the screen to make it invisible
      document.body.appendChild(el);                  // Append the <textarea> element to the HTML document
      const selected =            
        document.getSelection().rangeCount > 0        // Check if there is any content selected previously
          ? document.getSelection().getRangeAt(0)     // Store selection if found
          : false;                                    // Mark as false to know no selection existed before
      el.select();                                    // Select the <textarea> content
      document.execCommand('copy');                   // Copy - only works as a result of a user action (e.g. click events)
      document.body.removeChild(el);                  // Remove the <textarea> element
      if (selected) {                                 // If a selection existed before copying
        document.getSelection().removeAllRanges();    // Unselect everything on the HTML document
        document.getSelection().addRange(selected);   // Restore the original selection
      }
    };
    

    ps adding the source

    0 讨论(0)
  • 2020-11-27 03:25

    Try this .document.execCommand('copy')

    1. click the element and copy the text and post with tmp input element
    2. Then copy the text from this input

    function copy(that){
    var inp =document.createElement('input');
    document.body.appendChild(inp)
    inp.value =that.textContent
    inp.select();
    document.execCommand('copy',false);
    inp.remove();
    }
    <p onclick="copy(this)">hello man</p>

    0 讨论(0)
  • 2020-11-27 03:25

    This is the most suitable way to do it. It will copy all text in elements with the class "copy" in them.

    var copy = document.querySelectorAll(".copy"); 
    
    for (const copied of copy) { 
      copied.onclick = function() { 
        document.execCommand("copy"); 
      };  
      copied.addEventListener("copy", function(event) { 
        event.preventDefault(); 
        if (event.clipboardData) { 
          event.clipboardData.setData("text/plain", copied.textContent);
          console.log(event.clipboardData.getData("text"))
        };
      });
    };
    .copy {
                
      cursor: copy;
                
    }
    <p><span class="copy">Text</span></p>
    <p><span class="copy">More Text</span></p>
    <p><span class="copy">Even More Text</span></p>

    0 讨论(0)
提交回复
热议问题