当前位置:网站首页>ClipboardJS——开发学习总结1
ClipboardJS——开发学习总结1
2022-06-30 11:22:00 【Mr_LiuP】
问题需求描述:
由于是公司内部app,无法在app内复制、甚至截图,涉及安全隐私。
app内嵌H5页面,通过使用复制按钮,实现文本复制!
解决方法:
ClipboardJS,一款将文本复制到剪贴板的js插件,不使用flash,最小仅3k
1.使用方式:
npm安装:npm i clipboard --save
CDN安装:<script src="dist/clipboard.min.js"></script>
2.创建clipboard.js实例,通过传递 DOM 选择器、HTML 元素或 HTML 元素列表创建
<div id="temp_text">我是需要被复制的文字</div>
<button class="copy_btn" data-clipboard-target="#temp_text">一键复制</button>
var clipboard = new Clipboard('.copy_btn');
clipboard.on('success', function(e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
console.info('Trigger:', e.trigger);
e.clearSelection();
});
clipboard.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
3. 其他属性说明
data-clipboard-target:触发器元素的 data-clipboard-target 属性中包含的值需要与另一个元素的选择器相匹配
<div id="temp_text">我是需要被复制的文字</div>
<button class="copy_btn" data-clipboard-target="#temp_text">一键复制</button>
data-clipboard-text:不一定非要从另一个元素复制其内容。 也可以只在触发器元素中包含一个 data-clipboard-text 属性并复制该属性的内容
<button class="copy_btn" data-clipboard-text="我是被复制的文字">我复制我自己属性里的文字</button>
data-clipboard-action:指定是要复制还是剪切内容,值为 cut 表示执行剪切操作。如果省略此属性,默认情况下将使用复制
<div id="temp_text">我是需要被复制的文字</div>
<button class="copy_btn" data-clipboard-target="#temp_text" data-clipboard-action="cut">一键剪切</button>
边栏推荐
猜你喜欢
随机推荐
Introduction to game theory
Kotlin 协程调度切换线程是时候解开谜团了
学习redis实现分布式锁—–自己的一个理解
Kongsong (ICT Institute) - cloud security capacity building and trend in the digital age
一瓶水引发的“战争”
关于IP定位查询接口的测评Ⅲ
Typescript readonlyarray (read only array type) details
科普達人丨漫畫圖解什麼是eRDMA?
Livedata source code appreciation III - frequently asked questions
100 important knowledge points that SQL must master: using stored procedures
Alibaba cloud database represented by polardb ranks first in the world
What is a wechat applet that will open the door of the applet
100 important knowledge points that SQL must master: grouping data
"New digital technology" completed tens of millions of yuan of a + round financing and built an integrated intelligent database cloud management platform
脚本中如何'优雅'避免MySQL登录提示信息
一个人就是一本书
压缩状态DP位运算
He was the first hero of Shanghai's two major industries, but died silently in regret
1175. 质数排列
Use of switch statement in go language learning









