Home > Web Front-end > JS Tutorial > body text

JS to copy content to clipboard

php中世界最好的语言
Release: 2018-05-16 16:49:34
Original
2711 people have browsed it

This time I will bring you JS to copy content to the clipboard and JS to copy content to the clipboard. What are the precautions?. Here is a practical case. Let’s take a look.

Common methods

I checked the omnipotent Google, and now the common methods are mainly the following two:

Third-party library: clipboard.js
Native method: document.execCommand()

Let’s see how these two methods are used.

clipboard.js

Quote

Direct quote:

<script src="dist/clipboard.min.js"></script>
Copy after login

Package: npm install clipboard --save , then import Clipboard from 'clipboard';

Use

to copy from the input box
now There is an tag on the page, and we need to copy the content inside it. We can do this:

<input id="demoInput" value="hello world">
<button class="btn" data-clipboard-target="#demoInput">点我复制</button>
Copy after login
import Clipboard from &#39;clipboard&#39;;
const btnCopy = new Clipboard(&#39;btn&#39;);
Copy after login

Notice that a ## is added to the <button> tag #data-clipboard-target Attribute, its value is the id of the that needs to be copied. As the name suggests, it copies the content from the entire tag.

Direct copy

Sometimes, we don’t want to copy the content from , but just get the value directly from the variable. If we can do this in Vue:

<button class="btn" :data-clipboard-text="copyValue">点我复制</button>
Copy after login
import Clipboard from &#39;clipboard&#39;;
const btnCopy = new Clipboard(&#39;btn&#39;);
this.copyValue = 'hello world';
Copy after login

Event

Sometimes we need to do something after copying, then we need the

callback function support.

Add the following code to the processing function:

// 复制成功后执行的回调函数
clipboard.on('success', function(e) {
 console.info('Action:', e.action); // 动作名称,比如:Action: copy
 console.info('Text:', e.text); // 内容,比如:Text:hello word
 console.info('Trigger:', e.trigger); // 触发元素:比如:<button class="btn" :data-clipboard-text="copyValue">点我复制</button>
 e.clearSelection(); // 清除选中内容
});
// 复制失败后执行的回调函数
clipboard.on('error', function(e) {
 console.error('Action:', e.action);
 console.error('Trigger:', e.trigger);
});
Copy after login

Summary

The document also mentions that if

clipboard is used in a single page , in order to make life cycle management more elegant, remember btn.destroy() to destroy it after use.

clipboard is very simple to use. However, is it not elegant enough to use additional third-party libraries just for a copy function? What should we do at this time? Then use native methods to achieve it.

document.execCommand() method

Let’s first look at how this method is defined on

MDN:

which allows one to run commands to manipulate the contents of the editable region.
Copy after login

This means that commands can be run to manipulate the contents of the editable area. Note that it is an editable area.

Definition

bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
Copy after login

The method returns a Boolean value indicating whether the operation was successful.

  • aCommandName: Indicates the command name, such as: copy, cut, etc. (see commands for more commands);

  • aShowDefaultUI: Whether to display the user interface , usually false;

  • aValueArgument: Some commands require additional parameters, which are generally not used;

compatible Sex

The compatibility of this method was actually not very good before, but fortunately it is now basically compatible with all mainstream browsers and can also be used on mobile terminals.

Copy from the input box using

##Now there is an tag on the page that we want to copy For the content, we can do this:

<input id="demoInput" value="hello world">
<button id="btn">点我复制</button>
Copy after login

js code

const btn = document.querySelector(&#39;#btn&#39;);
btn.addEventListener(&#39;click&#39;, () => {
	const input = document.querySelector(&#39;#demoInput&#39;);
	input.select();
	if (document.execCommand(&#39;copy&#39;)) {
		document.execCommand(&#39;copy&#39;);
		console.log(&#39;复制成功&#39;);
	}
})
Copy after login

Copy elsewhere

Sometimes there is no

on the page

tag, we may need to copy the content from a <p>, or copy the variable directly. Remember in the definition of the

execCommand()

method that it can only operate on editable areas, which means that except for inputs like and