纯js实现复制剪切功能

所属分类:表单类

 3744次|    5次   点击收藏(0)

纯js实现复制剪切功能

用JavaScript拷贝、剪贴Textarea和Input里的数据

/* Textarea - Cut
<textarea id="bar">hello</textarea>
<button class="copy-button" data-clipboard-action="cut" data-clipboard-target="#bar">Cut</button>
*/ var clipboard = new Clipboard('.copy-button'); /* Input - Copy
<input id="foo" value="hello" type="text">
<button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>
*/ var clipboard = new Clipboard('.copy-button');

拷贝元素的innerHTML内容

/* HTMLElement - Copy
<div id="copy-target">hello</div>
<button class="copy-button" data-clipboard-action="copy" data-clipboard-target="#copy-target">Copy</button>
*/ var clipboard = new Clipboard('.copy-button');

TargetText的用法

// Contents of an element var clipboard = new Clipboard('.copy-button', { target: function() { return document.querySelector('#copy-target'); } }); // A specific string var clipboard = new Clipboard('.copy-button', { text: function() { return 'clipboard.js is awesome!'; } });

事件关联

var clipboard = new Clipboard('.btn'); clipboard.on('success', function(e) { console.log(e); }); clipboard.on('error', function(e) { console.log(e); });

关于前端资源库

前端资源库是haorooms博主平时积累的前端资源,大部分来源于互联网。但经过博主调试和加工,集聚了博主的心血。

因此,资源库所有资源,仅供学习和参考,切勿下载下来用于商业用途或者转卖。转载请注明来自haorooms博客资源库!

联系方式
关键词搜索