|
| 1 | +最近一个活动页面中有一个小需求,用户点击或者长按就可以复制内容到剪贴板,记录一下实现过程和遇到的坑。 |
| 2 | + |
| 3 | +## 常见方法 |
| 4 | + |
| 5 | +查了一下万能的Google,现在常见的方法主要是以下两种: |
| 6 | + |
| 7 | +- 第三方库:clipboard.js |
| 8 | +- 原生方法:document.execCommand() |
| 9 | + |
| 10 | +分别来看看这两种方法是如何使用的。 |
| 11 | + |
| 12 | +## clipboard.js |
| 13 | + |
| 14 | +这是clipboard的官网:[https://clipboardjs.com/](https://clipboardjs.com/),看起来就是这么的简单。 |
| 15 | + |
| 16 | +### 引用 |
| 17 | + |
| 18 | +直接引用: `<script src="dist/clipboard.min.js"></script>` |
| 19 | + |
| 20 | +包: `npm install clipboard --save` ,然后 `import Clipboard from 'clipboard';` |
| 21 | + |
| 22 | +### 使用 |
| 23 | + |
| 24 | +#### 从输入框复制 |
| 25 | + |
| 26 | +现在页面上有一个 `<input>` 标签,我们需要复制其中的内容,我们可以这样做: |
| 27 | + |
| 28 | +```html |
| 29 | +<input id="demoInput" value="hello world"> |
| 30 | +<button class="btn" data-clipboard-target="#demoInput">点我复制</button> |
| 31 | +``` |
| 32 | + |
| 33 | +```javascript |
| 34 | +import Clipboard from 'clipboard'; |
| 35 | +const btnCopy = new Clipboard('btn'); |
| 36 | +``` |
| 37 | + |
| 38 | +注意到,在 `<button>` 标签中添加了一个 `data-clipboard-target` 属性,它的值是需要复制的 `<input>` 的 `id`,顾名思义是从整个标签中复制内容。 |
| 39 | + |
| 40 | +#### 直接复制 |
| 41 | + |
| 42 | +有的时候,我们并不希望从 `<input>` 中复制内容,仅仅是直接从变量中取值。如果在 `Vue` 中我们可以这样做: |
| 43 | + |
| 44 | +```html |
| 45 | +<button class="btn" :data-clipboard-text="copyValue">点我复制</button> |
| 46 | +``` |
| 47 | + |
| 48 | +```javascript |
| 49 | +import Clipboard from 'clipboard'; |
| 50 | +const btnCopy = new Clipboard('btn'); |
| 51 | +this.copyValue = 'hello world'; |
| 52 | +``` |
| 53 | + |
| 54 | +#### 事件 |
| 55 | + |
| 56 | +有的时候我们需要在复制后做一些事情,这时候就需要回调函数的支持。 |
| 57 | + |
| 58 | +在处理函数中加入以下代码: |
| 59 | + |
| 60 | +```javascript |
| 61 | +// 复制成功后执行的回调函数 |
| 62 | +clipboard.on('success', function(e) { |
| 63 | + console.info('Action:', e.action); // 动作名称,比如:Action: copy |
| 64 | + console.info('Text:', e.text); // 内容,比如:Text:hello word |
| 65 | + console.info('Trigger:', e.trigger); // 触发元素:比如:<button class="btn" :data-clipboard-text="copyValue">点我复制</button> |
| 66 | + e.clearSelection(); // 清除选中内容 |
| 67 | +}); |
| 68 | + |
| 69 | +// 复制失败后执行的回调函数 |
| 70 | +clipboard.on('error', function(e) { |
| 71 | + console.error('Action:', e.action); |
| 72 | + console.error('Trigger:', e.trigger); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +### 小结 |
| 77 | + |
| 78 | +文档中还提到,如果在单页面中使用 `clipboard` ,为了使得生命周期管理更加的优雅,在使用完之后记得 `btn.destroy()` 销毁一下。 |
| 79 | + |
| 80 | +`clipboard` 使用起来是不是很简单。但是,就为了一个 `copy` 功能就使用额外的第三方库是不是不够优雅,这时候该怎么办?那就用原生方法实现呗。 |
| 81 | + |
| 82 | +## document.execCommand()方法 |
| 83 | + |
| 84 | +先看看这个方法在 `MDN` 上是怎么定义的: |
| 85 | + |
| 86 | +> which allows one to run commands to manipulate the contents of the editable region. |
| 87 | +
|
| 88 | +意思就是可以允许运行命令来操作可编辑区域的内容,注意,是**可编辑区域**。 |
| 89 | + |
| 90 | +### 定义 |
| 91 | + |
| 92 | +> bool = document.execCommand(aCommandName, aShowDefaultUI, aValueArgument) |
| 93 | +
|
| 94 | +方法返回一个 `Boolean` 值,表示操作是否成功。 |
| 95 | + |
| 96 | +- `aCommandName` :表示命令名称,比如: `copy`, `cut` 等(更多命令见[命令](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#%E5%91%BD%E4%BB%A4)); |
| 97 | +- `aShowDefaultUI`:是否展示用户界面,一般情况下都是 `false`; |
| 98 | +- `aValueArgument`:有些命令需要额外的参数,一般用不到; |
| 99 | + |
| 100 | +### 兼容性 |
| 101 | + |
| 102 | +这个方法在之前的兼容性其实是不太好的,但是好在现在已经基本兼容所有主流浏览器了,在移动端也可以使用。 |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +### 使用 |
| 107 | + |
| 108 | +#### 从输入框复制 |
| 109 | + |
| 110 | +现在页面上有一个 `<input>` 标签,我们想要复制其中的内容,我们可以这样做: |
| 111 | + |
| 112 | +```html |
| 113 | +<input id="demoInput" value="hello world"> |
| 114 | +<button id="btn">点我复制</button> |
| 115 | +``` |
| 116 | + |
| 117 | +```javascript |
| 118 | +const btn = document.querySelector('#btn'); |
| 119 | +btn.addEventListener('click', () => { |
| 120 | + const input = document.querySelector('#demoInput'); |
| 121 | + input.select(); |
| 122 | + if (document.execCommand('copy')) { |
| 123 | + document.execCommand('copy'); |
| 124 | + console.log('复制成功'); |
| 125 | + } |
| 126 | +}) |
| 127 | +``` |
| 128 | + |
| 129 | +#### 其它地方复制 |
| 130 | + |
| 131 | +有的时候页面上并没有 `<input>` 标签,我们可能需要从一个 `<div>` 中复制内容,或者直接复制变量。 |
| 132 | + |
| 133 | +还记得在 `execCommand()` 方法的定义中提到,它只能操作**可编辑区域**,也就是意味着除了 `<input>`、`<textarea>` 这样的输入域以外,是无法使用这个方法的。 |
| 134 | + |
| 135 | +这时候我们需要曲线救国。 |
| 136 | + |
| 137 | +```html |
| 138 | +<button id="btn">点我复制</button> |
| 139 | +``` |
| 140 | + |
| 141 | +```javascript |
| 142 | +const btn = document.querySelector('#btn'); |
| 143 | +btn.addEventListener('click',() => { |
| 144 | + const input = document.createElement('input'); |
| 145 | + document.body.appendChild(input); |
| 146 | + input.setAttribute('value', '听说你想复制我'); |
| 147 | + input.select(); |
| 148 | + if (document.execCommand('copy')) { |
| 149 | + document.execCommand('copy'); |
| 150 | + console.log('复制成功'); |
| 151 | + } |
| 152 | + document.body.removeChild(input); |
| 153 | +}) |
| 154 | +``` |
| 155 | + |
| 156 | +算是曲线救国成功了吧。在使用这个方法时,遇到了几个坑。 |
| 157 | + |
| 158 | +#### 遇到的坑 |
| 159 | + |
| 160 | +在Chrome下调试的时候,这个方法时完美运行的。然后到了移动端调试的时候,坑就出来了。 |
| 161 | + |
| 162 | +对,没错,就是你,ios。。。 |
| 163 | + |
| 164 | +1. 点击复制时屏幕下方会出现白屏抖动,仔细看是拉起键盘又瞬间收起 |
| 165 | + |
| 166 | + 知道了抖动是由于什么产生的就比较好解决了。既然是拉起键盘,那就是聚焦到了输入域,那只要让输入域不可输入就好了,在代码中添加 `input.setAttribute('readonly', 'readonly');` 使这个 `<input>` 是只读的,就不会拉起键盘了。 |
| 167 | + |
| 168 | +2. 无法复制 |
| 169 | + |
| 170 | + 这个问题是由于 `input.select()` 在ios下并没有选中全部内容,我们需要使用另一个方法来选中内容,这个方法就是 `input.setSelectionRange(0, input.value.length);`。 |
| 171 | + |
| 172 | +完整代码如下: |
| 173 | + |
| 174 | +```javascript |
| 175 | +const btn = document.querySelector('#btn'); |
| 176 | +btn.addEventListener('click',() => { |
| 177 | + const input = document.createElement('input'); |
| 178 | + input.setAttribute('readonly', 'readonly'); |
| 179 | + input.setAttribute('value', 'hello world'); |
| 180 | + document.body.appendChild(input); |
| 181 | + input.setSelectionRange(0, 9999); |
| 182 | + if (document.execCommand('copy')) { |
| 183 | + document.execCommand('copy'); |
| 184 | + console.log('复制成功'); |
| 185 | + } |
| 186 | + document.body.removeChild(input); |
| 187 | +}) |
| 188 | +``` |
| 189 | + |
| 190 | +## 总结 |
| 191 | + |
| 192 | +以上就是关于JavaScript如何实现复制内容到剪贴板,附上几个链接: |
| 193 | + |
| 194 | +[execCommand MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) |
| 195 | + |
| 196 | +[execCommand兼容性](https://caniuse.com/#search=execCommand) |
| 197 | + |
| 198 | +[clipboard.js](https://github.com/zenorocha/clipboard.js) |
0 commit comments