How to copy content to clipboard in JavaScript
javascript
js
clipboard
<p>There is a small requirement in a recent event page. Users can click or long-press to copy the content to the clipboard and record the implementation process and pitfalls encountered. Friends who need it can refer to it
<p>Common methods
<p> After checking the almighty Google, the common methods are mainly the following two:
<p>Third-party library: clipboard.js
Native method: document. execCommand() <p>Let’s see how these two methods are used. <p>clipboard.js <p>This is the official website of clipboard: https://clipboardjs.com/, it seems so simple. <p>Quote <p>Direct quote:<p>Package:
Now there is an <input> tag on the page, we need to copy it Content, we can do this:<p><p><p>Notice that in <p><p>Event<p>Sometimes we need to copy To do something, you need the support of callback function. <p>Add the following code to the processing function: <p><p>##SummaryThe document also mentions Yes, if you use <p>clipboard
means that you can allow one to run commands to manipulate the contents of the editable region. Note that it is an editable region. <p>Definition<p>bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)<p>The method returns a Boolean value indicating whether the operation was successful. <p>
<p>Copy from the input box using ##Now there is an <input> tag on the page that we want to copy For the content, we can do this: <p>
<p>js code<p><p>Copy elsewhere<p>Sometimes there is no <p> tag on the page, we may need to copy from a
Native method: document. execCommand() <p>Let’s see how these two methods are used. <p>clipboard.js <p>This is the official website of clipboard: https://clipboardjs.com/, it seems so simple. <p>Quote <p>Direct quote:
<script src="dist/clipboard.min.js"></script>
Copy after login
npm install clipboard --save
, then import Clipboard from 'clipboard'
;<p>Use<p>Copy from the input boxNow there is an <input> tag on the page, we need to copy it Content, we can do this:
<input id="demoInput" value="hello world"> <button class="btn" data-clipboard-target="#demoInput">点我复制</button>
Copy after login
import Clipboard from 'clipboard'; const btnCopy = new Clipboard('btn');
Copy after login
<button> A
data-clipboard-target attribute is added to the
tag. Its value is the id
of the <input>
that needs to be copied. As the name suggests, it is from Copy content throughout the tag. <p>Direct copy<p>Sometimes, we don’t want to copy the content from <input>, 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 'clipboard'; const btnCopy = new Clipboard('btn'); this.copyValue = 'hello world';
Copy after login
// 复制成功后执行的回调函数 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
in a single page, in order to make life cycle management more elegant, remember to
btn.destroy() 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. <p><p>document.execCommand() methodLet’s first look at how this method is defined on <p>MDN:
which allows one to run commands to manipulate the contents of the editable region. <p>means that you can allow one to run commands to manipulate the contents of the editable region. Note that it is an editable region. <p>Definition<p>bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)<p>The method returns a Boolean value indicating whether the operation was successful. <p>
- aCommandName: Indicates the command name, such as: copy, cut, etc. (see commands for more commands); <p>
- aShowDefaultUI: Whether to display the user interface , usually false; <p>
- aValueArgument: Some commands require additional parameters, which are generally not used; <p>

<input id="demoInput" value="hello world"> <button id="btn">点我复制</button>
Copy after login
const btn = document.querySelector('#btn'); btn.addEventListener('click', () => { const input = document.querySelector('#demoInput'); input.select(); if (document.execCommand('copy')) { document.execCommand('copy'); console.log('复制成功'); } })
Copy after login
<p>
content, or copy the variable directly.
Remember in the definition of the execCommand() <p> method that it can only operate on editable areas, which means that except for inputs like <input> and