PC端项目很多都会用到复制到粘贴板的功能,但是由于浏览器的差异(IE/ff)和支持情况的而不同导致各种兼容
本来一直用的zeroClipboard插件,这个插件主要用了flash来实现,flash可以绕过浏览器不兼容的问题,兼容性还不错,但是依赖浏览器flash插件,有些用户可能会为了禁掉各种广告而禁掉flash插件,甚至FF浏览器本身就对flash插件进行了限制,导致有客户来问,所以研究了下相关内容;
这里介绍一套解决方案:clipBoard.js,这个插件纯js实现,主要原理是用到了document.execCommand('copy')方法,使用详解,这个方法可以鼠标选中的内容;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16简单的实现:
<input type="text" id="input1">
    <input type="button" Value="click" onclick="copy()">
    <script>
        function copy () {
            var input = document.createElement('input');
            input.value = document.getElementById('input1').value;
            input.style = "opacity:0"
            document.body.appendChild(input);
            input.select();
            document.execCommand('copy');
            console.log('Copy!');
            input.remove();
        }
    </script>
下面介绍一整套解决方案:
判断浏览器
1  | //getClient为判断浏览器 方法地址:/resources/js/component.js  | 
