1.js复制
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <title>复制功能</title> |
| </head> |
| |
| <body> |
| <span id="copyText1">这里是被复制的内容</span> |
| <input type="button" onClick="copy1()" value="点击复制"/> |
| |
| <span id="copyText2">44545123664444446666666</span> |
| <input type="button" value="复制" onclick="copy2(document.getElementById('copyText2'));" /> |
| </body> |
| |
| <script type="text/javascript"> |
| function copy1() { |
| var copy = document.getElementById("copyText1").innerText; |
| var oInput = document.createElement('input'); |
| oInput.value = copy; |
| document.body.appendChild(oInput); |
| oInput.select(); |
| document.execCommand("Copy"); |
| oInput.className = 'oInput'; |
| oInput.style.display = 'none'; |
| alert('复制成功'); |
| } |
| |
| function copy2(x) { |
| if (document.selection) { |
| var range = document.body.createTextRange(); |
| range.moveToElementText(x); |
| range.select(); |
| } else if (window.getSelection) { |
| var selection = window.getSelection(); |
| var range = document.createRange(); |
| selection.removeAllRanges(); |
| range.selectNodeContents(x); |
| selection.addRange(range); |
| } |
| document.execCommand("copy"); |
| } |
| |
| </script> |
| |
| </html> |
复制
2.clipboard复制
| 使用npm工具安装 |
| |
| npm install clipboard --save |
| |
| 下载压缩包,链接:https://github.com/zenorocha/clipboard.js/archive/master.zip |
复制
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>clipboard基本的使用方法</title> |
| </head> |
| |
| <body> |
| |
| |
| |
| |
| <input id="foo" type="text" value="这是input元素的value值" /> |
| <button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">点击复制</button> |
| |
| |
| <textarea id="bar">这里的文本可被复制或者剪切</textarea> |
| <button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">点击剪切文字</button> |
| |
| |
| <div>这是div元素里的文字</div> |
| <button class="btn" data-clipboard-action="copy" data-clipboard-target="div">复制来自div的文字</button> |
| |
| |
| <button class="btn" data-clipboard-text="这里的文字将被复制">复制自己的文字</button> |
| </body> |
| |
| <script src="./node_modules/clipboard/dist/clipboard.min.js"></script> |
| |
| |
| <script> |
| |
| var clipboard = new ClipboardJS('.btn'); |
| |
| |
| clipboard.on('success', function (e) { |
| console.log(e); |
| |
| console.info('Action:', e.action); |
| |
| console.info('Text:', e.text); |
| |
| console.info('Trigger:', e.trigger); |
| }); |
| |
| |
| clipboard.on('error', function (e) { |
| console.log(e); |
| console.error('Action:', e.action); |
| console.error('Trigger:', e.trigger); |
| }); |
| </script> |
| |
| </html> |
复制
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>clipboard高级的使用方法1</title> |
| </head> |
| |
| <body> |
| |
| |
| <button class="btn">点击复制</button> |
| <div>这里的内容将被复制</div> |
| |
| |
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script> |
| |
| <script> |
| |
| var clipboard = new ClipboardJS('.btn', { |
| target: function () { |
| |
| return document.querySelector('div'); |
| |
| |
| |
| |
| |
| |
| |
| }, |
| }); |
| |
| |
| clipboard.on('success', function (e) { |
| console.log(e); |
| }); |
| |
| |
| clipboard.on('error', function (e) { |
| console.log(e); |
| }); |
| </script> |
| </body> |
| |
| </html> |
复制
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>clipboard高级的使用方法2</title> |
| </head> |
| |
| <body> |
| |
| <button class="btn">Copy</button> |
| |
| |
| |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js"></script> |
| |
| <script> |
| |
| var clipboard = new ClipboardJS('.btn', { |
| text: function () { |
| return '这里的文本将被复制'; |
| }, |
| }); |
| |
| |
| clipboard.on('success', function (e) { |
| console.log(e); |
| }); |
| |
| |
| clipboard.on('error', function (e) { |
| console.log(e); |
| }); |
| </script> |
| </body> |
| |
| </html> |
复制