项目中需要实现一个点击按钮复制链接的功能,网上看到的几款插件,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();
}
};
标签:
JavaScript,复制,剪切
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“JavaScript实现复制或剪切内容到剪贴板功能的方法”评论...
更新日志
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]