[英]Clipboard.js loop buttons
I use clipboard.js and after a click on one of my buttons I want to show a success message under the button, but I am not able to loop throught. 我使用剪贴板.js,单击我的一个按钮后,我想在该按钮下显示成功消息,但无法循环显示。
This is the button: 这是按钮:
<button type="button" class="clipboard-button button-rect" data-clipboard-text="{{ site.author.email|safe_email }}">
<span class="button-text">Get in touch</span>
<span class="clipboard-message">My E-Mail has been copied</span>
</button>
And this the js: 而这js:
var clipboard = new ClipboardJS('.clipboard-button');
clipboard.on('success', function () {
var message = document.querySelectorAll('.clipboard-message');
message.style.opacity = '1';
setTimeout(function () {
message.style.opacity = '0';
}, 2000);
});
I want to show the message under the button which was pressed and not for every button. 我想在按下的按钮下显示消息,而不是每个按钮都显示。
I would appreciate your help, I tried so much. 感谢您的帮助,我做了很多尝试。 I'm an JS beginner btw, so be patient please.
我是JS初学者,所以请耐心等待。 :)
:)
On thing you should look at is achieving to retrieve the clicked DOM element (a button in this case). 实际上,您应该实现检索单击的DOM元素(在这种情况下为按钮)。 It will then be easy to find the corresponding
.clipboard-message
span element. 然后,将很容易找到相应的
.clipboard-message
span元素。
To retrieve the clicked element you can use the parameter given to the success
event callback function, like stated here: https://clipboardjs.com/#events 要检索被单击的元素,可以使用赋予
success
事件回调函数的参数,如下所示: https : //clipboardjs.com/#events
Then your JavaScript code becomes: 然后,您的JavaScript代码变为:
var clipboard = new ClipboardJS('.clipboard-button');
clipboard.on('success', function (e) {
// e.trigger corresponds to the clipboard-button DOM element that triggered the event
// You can then use querySelector(...) to retrieve to first child element with the class clipboard-message
var message = e.trigger.querySelector('.clipboard-message');
message.style.opacity = '1';
setTimeout(function () {
message.style.opacity = '0';
}, 2000);
});
Here is a working code snippet for you to try: 这是一个工作代码段供您尝试:
var clipboard = new ClipboardJS('.clipboard-button'); clipboard.on('success', function (e) { var message = e.trigger.querySelector('.clipboard-message'); message.style.opacity = '1'; setTimeout(function () { message.style.opacity = '0'; }, 2000); });
.clipboard-message { color: green; opacity: 0; }
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script> <button type="button" class="clipboard-button button-rect" data-clipboard-text="{{ site.author.email|safe_email }}"> <span class="button-text">Get in touch</span> <span class="clipboard-message">My E-Mail has been copied</span> </button> <button type="button" class="clipboard-button button-rect" data-clipboard-text="{{ site.author.email|safe_email }}"> <span class="button-text">Here is your ID</span> <span class="clipboard-message">My ID has been copied</span> </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.