前提・実現したいこと
clipboard.jsを使用して色々と試しています。
clipboard.js公式サイト、参考サイト
https://github.com/zenorocha/clipboard.js
https://clipboardjs.com/
以下のようなhtmlとindex.jsを用意しました。
pushボタン押したら、クリップボードのsuccess・errorのイベントを登録し、コピーボタンを押したらテキストの内容がコピーされ、alertが出る仕組みとなっています。
このときにpushボタンを複数回押したら、そのクリップボードのイベントが何回も登録され、登録された分のalertが表示されますが、その登録したイベントを削除することが可能でしょうか?
どなたか教えてください。
よろしくお願いします。
html
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta charset="utf-8" /> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/clipboard.min.js"></script> <script src="~/Scripts/index.js"></script> </head> <div class="row"> <input type="text" class="copyText" value="コピーしたよ" readonly /> <button type="button" class="copyButton" data-clipboard-target=".copyText">コピー</button> <button type="button" class="pushButton" >push</button> </div>
index.js
$(function () { $('.pushButton').on('click', function () { var clipboard = new Clipboard('.copyButton'); clipboard.on('success', function (e) { alert('コピーsucess'); }); clipboard.on('error', function (e) { alert('コピーerror'); }); }); });
回答1件
あなたの回答
tips
プレビュー