<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:
<script src="dist/clipboard.min.js"></script>
Copy after login
<p>Package: npm install clipboard --save , then import Clipboard from 'clipboard';<p>Use<p>Copy from the input box Now there is an <input> tag on the page, we need to copy it Content, we can do this:
import Clipboard from 'clipboard';
const btnCopy = new Clipboard('btn');
Copy after login
<p><p>Notice that in <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:
import Clipboard from 'clipboard';
const btnCopy = new Clipboard('btn');
this.copyValue = 'hello world';
Copy after login
<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 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>
<p> compatible SexThe compatibility of this method was not very good before, but fortunately it is now basically compatible with all mainstream browsers and can also be used on mobile terminals. <p><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>Copy elsewhere<p>Sometimes there is no <p> tag on the page, we may need to copy from a <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
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn