前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决
下载地址:下载地址: https://github.com/zenorocha/clipboard.js
cnd 引入js : https://cdn.staticfile.org/clipboard.js/1.5.15/clipboard.min.js
html部分:
<div id="foo">需要被粘贴的内容</div> <!-- 复制按钮 --> <a class="copyBtn" href="javascript:" data-clipboard-action="copy" data-clipboard-target="#foo"></a>
js部分:
var clipboard = new Clipboard('.copyBtn').on('success', function(e) { var e = e || window.event; console.log('复制成功!!!'); }).on('error', function(e) {});
完整
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>iOS复制兼容</title> </head> <body> <div id="foo">需要被粘贴的内容</div> <!-- 复制按钮 --> <a class="copyBtn" href="javascript:" data-clipboard-action="copy" data-clipboard-target="#foo"></a> <script type="text/javascript" src="https://cdn.staticfile.org/clipboard.js/1.5.15/clipboard.min.js"></script> <script> window.onload = function(){ var clipboard = new Clipboard('.copyBtn').on('success', function(e) { var e = e || window.event; console.log('复制成功!!!'); }).on('error', function(e) {}); } </script> </body> </html>