Detailed explanation of JavaScript clipboard usage

Detailed explanation of JavaScript clipboard usage

(1) Introduction:

clipboard.js is a lightweight JavaScript plugin that implements the function of copying text to the clipboard. This plug-in can copy text contents such as input boxes, text fields, and text in DIV elements to the clipboard
clipboard.js supports mainstream browsers: Chrome 42+; Firefox 41+; IE 9+; Opera 29+; Safari 10+;

(2) The ways to copy content on the clipboard are:

  1. Copy the target content from target
  2. The content to be copied by function
  3. Return the copied content through attributes

target copies the target content, which is not discussed here. Let’s just talk about the operation of function and attributes.

(3) There are two types of Function operations:

The first one:

The target function copies the content. The target specifies the node to be copied. Here, the return value is 'hello'.

   <button class="btn">Copy_target</button>
   <div>hello</div>

   <script>
   var clipboard = new Clipboard('.btn', {
   // Specify the node to be copied through target target: function() {
                  return document.querySelector('div');
             }
   });

   clipboard.on('success', function(e) {
       console.log(e);
   });

   clipboard.on('error', function(e) {
       console.log(e);
   });
   </script>

Second type:

Copy content through the text function
The copy content specified by the text function, here returns 'to be or not to be'

<button class="btn">Copy</button>
<script>
   var clipboard = new Clipboard('.btn', {
   // Click the copy button and return the copied content directly through text text: function() {
           return 'to be or not to be';
       }
   });

   clipboard.on('success', function(e) {
       console.log(e);
   });

   clipboard.on('error', function(e) {
       console.log(e);
});

(4) Return the copied content through attributes

Type 1: Single Node

Specify the node object by id and send it to Clipboard as a parameter. The return value here is the content of data-clipboard-text

// Get the content of data-clipboard-text by id <div id="btn" data-clipboard-text="1">
        <span>Copy</span>
</div>
 
<script>
    var btn = document.getElementById('btn');
    var clipboard = new Clipboard(btn);
 
    clipboard.on('success', function(e) {
        console.log(e);
    });
 
    clipboard.on('error', function(e) {
        console.log(e);
    });
</script>

Second: Multi-node

Get all buttons through class and send them to Clipboard as parameters. When each button is clicked, the return value is the content of its corresponding data-clipboard-text, which are 1, 2, and 3 respectively.

// Register multiple buttons through class and get the value of data-clipboard-text <button class="btn" data-clipboard-text="1">Copy</button>
    <button class="btn" data-clipboard-text="2">Copy</button>
    <button class="btn" data-clipboard-text="3">Copy</button>
 <script>
    var clipboard = new Clipboard('.btn');
 
    clipboard.on('success', function(e) {
        console.log(e);
    });
 
    clipboard.on('error', function(e) {
        console.log(e);
    });
    </script>

(5) Compatibility between functions and attributes

function:

//ClipboardJS.isSupported() //--------This sentence is: Is it compatible? var clipboard = new Clipboard('.btn');
// Graceful downgrade: If Safari version number >= 10, the prompt is that the copy is successful; otherwise, it prompts that you need to manually select "Copy" to copy after selecting the text clipboard.on('success', function(e) {
    alert('Copy successful!')
    e.clearSelection();
});
clipboard.on('error', function(e) {
    alert('Please select "Copy" to copy!')
//Try to remove the alert, the system's "Copy" tool can pop up, but you need to click the button twice to pop up. The specific reason is not clear, refer to the picture above. Some people say that you can try to write an empty click event at the trigger location, οnclick="" because iOS does not simply support on events}); 

property:

<img
   src="../../../../assets/images/zuop_award/copy_link.png"
      @click="copy"
      data-clipboard-action="copy"
      class="email"
      :data-clipboard-text="'[email protected]'"
    /> 

-------------------
  copy() {
     var clipboard = new Clipboard(".email")
     // console.log(clipboard);
     clipboard.on("success", e => {
       // console.log("Copy successful", e);
       Toast({
         message: "Copy successful"
       })
       // Release memory clipboard.destroy()
     })
     clipboard.on("error", e => {
       // Does not support copying Toast({
         message: "Mobile phone permissions do not support the copy function"
       })
       console.log("This browser does not support automatic copy")
       // Release memory clipboard.destroy()
     })
   }

This is the end of this article about the detailed usage of JavaScript clipboard. For more relevant JavaScript clipboard content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use the Clipboard API in JS
  • Code block highlighting can be copied and displayed js plug-in highlight.js + clipboard.js integration
  • Vue uses Clipboard.JS to copy content examples in h5 pages
  • Example of using clipboard.js in vue to copy text with one click
  • JS plugin clipboard.js realizes one-click copy and paste function
  • Solution to clipboard.js copy failure on mobile terminal
  • Example code for implementing the copy function using clipboard.js
  • ZeroClipboard.js uses one flash to copy multiple text boxes

<<:  MySql implements page query function

>>:  How to view files in Docker image

Recommend

Use nginx + secondary domain name + https support

Step 1: Add a secondary domain name to the Alibab...

CSS 3.0 text hover jump special effects code

Here is a text hovering and jumping effect implem...

How to play local media (video and audio) files using HTML and JavaScript

First of all, for security reasons, JavaScript ca...

CSS3 click button circular progress tick effect implementation code

Table of contents 8. CSS3 click button circular p...

HTML table tag tutorial (27): cell background image attribute BACKGROUND

We can set a background image for the cell, and w...

Use of Linux ln command

1. Command Introduction The ln command is used to...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

How to set up cross-domain access in IIS web.config

Requirement: The page needs to display an image, ...

How to configure domestic sources in CentOS8 yum/dnf

CentOS 8 changed the software package installatio...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

Solution to the conflict between Linux kernel and SVN versions

Phenomenon The system could compile the Linux sys...

MySQL Tutorial: Subquery Example Detailed Explanation

Table of contents 1. What is a subquery? 2. Where...

Vue local component data sharing Vue.observable() usage

As components become more detailed, you will enco...