猿问

如何循环赋值,用clipboard.js实现多个button复制多个相对应的内容

问题阐述:有15个div分别含有不同代码片段sqlcode[i],每个div下有一个复制按钮btn[i],同时按钮复制成功会显示一个check[i]的图标。现在,想简化代码,请大牛指点!!!

html源码

js源码

https://img3.mukewang.com/5c2473c90001198206100571.jpg

富国沪深
浏览 2964回答 1
1回答

慕的地6264312

你的复制的按钮用class来写<input class='copy_item' id='copy_item1' readonly value='你要复制的值' style='border: none;background:none;box-shadow: none;width: auto;' />点击复制js如下var doms = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('.copy-btn').each(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doms.push(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var clip = new ZeroClipboard(doms);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //设置aftercopy事件让复制成功后弹窗提示&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clip.on('aftercopy', function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertx('复制成功');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clip.on('error', function (event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this.hide().before(event.message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZeroClipboard.destroy();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答