JavaScript实现复制或剪切内容到剪贴板功能的方法
项目中需要实现一个点击按钮复制链接的功能,网上看到的几款插件,ZeroClipboard是通过flash实现的复制功能,随着越来越多的提议废除flash,能不能通过JS来实现复制剪切呢,今天分享一个兼容IE7浏览器复制的插件给大家,支持使用javascript实现复制、剪切和粘贴。
方法。
复制
var copy = new clipBoard(document.getElementById('data'), { beforeCopy: function() { }, copy: function() { return document.getElementById('data').value; }, afterCopy: function() { } });
复制将自动被调用,如果你想要自己调用:
var copy = new clipBoard(document.getElementById('data')); copy.copyd();
document.getElementById('data') :要获取的对象, 你也可以使用jQuery $('#data')
剪切
基本上与复制的实现方法相同:
var cut = new clipBoard(document.getElementById('data'), { beforeCut: function() { }, cut: function() { return document.getElementById('data').value; }, afterCut: function() { } });
或者
var cut = new clipBoard(document.getElementById('data')); cut.cut(); paste var paste = new clipBoard(document.getElementById('data'), { beforePaste: function() { }, paste: function() { return document.getElementById('data').value; }, afterPaste: function() { } });
或者
var paste = new clipBoard(document.getElementById('data')); paste.paste();
完整代码:
(function(name, fun) { if (typeof module !== 'undefined' && module.exports) { module.exports = fun(); } else if (typeof define === 'function' && define.amd) { define(fun); } else { this[name] = fun(); } })('clipBoard', function() { "use strict"; function clipBoard(tar, options) { this.options = options || {}; this.tar = tar[0] || tar; // if options contain copy, copy will be applied soon if (this.options.copy) { this.copyd(); } if(this.options.cut) { this.cut(); } if(this.options.paste) { this.paste(); } } clipBoard.prototype.copyd = function(value) { // before the copy it will be called, you can check the value or modify the value if (this.options.beforeCopy) { this.options.beforeCopy(); } // if the options set copy function, the value will be set. then get the paramer value. // above all, if the value is null, then will be set the tar of value value = value || this.tar.value || this.tar.innerText; if (this.options.copy) { value = this.options.copy(); } // for modern browser if (document.execCommand) { var element = document.createElement('SPAN'); element.textContent = value; document.body.appendChild(element); if (document.selection) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var range = document.createRange(); range.selectNode(element); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); } document.execCommand('copy'); element.remove ? element.remove() : element.removeNode(true); } // for ie if (window.clipboardData) { window.clipboardData.setData('text', value); } // after copy if (this.options.afterCopy) { this.options.afterCopy(); } };
相关文章
JS 逻辑判断不要只知道用 if-else 和 switch条件判断(小技巧)
这篇文章主要介绍了JS 逻辑判断不要只知道用 if-else 和 switch,在一些逻辑复杂度的增加,代码中的 if/else 和 switch 会越来越臃肿。本文将带你尝试写出更优雅的判断逻辑,需要的朋友可以参考下2020-05-05chrome浏览器当表单自动填充时如何去除浏览器自动添加的默认样式
很多朋友都遇到这个问题:当使用chrome浏览器表单自动填充时都会自动添加默认的样式,该如何去除默认样式呢?看看小编是怎么去除的,需要的朋友一起学习吧2015-10-10JS实现预加载视频音频/视频获取截图(返回canvas截图)
这篇文章主要介绍了JS实现预加载视频音频/视频获取截图(返回canvas截图)的相关资料,需要的朋友可以参考下2017-10-10javascript提取URL的搜索字符串中的参数(自定义函数实现)
我们经常会看到有的页面链接地址后面会跟有参数,很多时候我们需要获得这些参数的值,接下来将介绍获取方法,感兴趣的朋友可以了解系,希望本文对你有所帮助2013-01-01使用纯前端JavaScript实现Excel导入导出方法过程详解
这篇文章主要介绍了使用纯前端JavaScript实现Excel导入导出方法过程详解,文章通过示例代码和图文解析介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-08-08
最新评论