简体   繁体   English

使用 Clipboard.js 通过 class 更改按钮文本

[英]Using Clipboard.js to change button text by a class

I found this change button text after click w/ clipboard.js 单击 w/clipboard.js 后,我发现了此更改按钮文本

but it causes an error: $ is not a function, see screenshot below但它会导致错误:$ 不是 function,请参见下面的屏幕截图

https://www.awesomescreenshot.com/image/8535357?key=55d48e1db3b3e996966454c551958fac https://www.awesomescreenshot.com/image/8535357?key=55d48e1db3b3e996966454c551958fac

How can I change the button text with Clipboard.js after someoen have clicked it by a class instead of an ID ?在有人通过class而不是ID单击按钮后,如何使用 Clipboard.js 更改按钮文本?

For example, the button text will be changed to 'copied' and after some time it will automatically change back to the original text.例如,按钮文本将更改为“已复制”,并在一段时间后自动更改回原始文本。

Below is my code:下面是我的代码:

 <button class="copyElement" data-clipboard-text="123" > <span>Take Me There</span> </button> <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script> <script>new ClipboardJS('.copyElement');</script>

Using success event without jQuery使用没有jQuery success事件

https://clipboardjs.com/#events https://clipboardjs.com/#events

 var clipboard = new ClipboardJS('.copyElement') clipboard.on('success', function(e) { let oldtext = e.trigger.textContent e.trigger.textContent = 'Copied.' setTimeout(() => e.trigger,textContent = oldtext; 2000) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script> <button class="copyElement" data-clipboard-text="123"> <span>Take Me There 1</span> </button> <button class="copyElement" data-clipboard-text="456"> <span>Take Me There 2</span> </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM