![](/img/trans.png)
[英]Execute javascript function on anchor click, but the code must run on the page opened by that anchor
[英]Run javascript code which is in HTML if users click anchor
該代碼用於復制功能。 當用戶單擊錨點時,copyText 應保存為剪貼板
function myFunc { $('#link-button-1608115397633').click(function (e) { e.preventDefault(); var copyText = "LASTCHANCE1"; document.addEventListener('copy', function(e) { e.clipboardData.setData('text/plain', copyText); e.preventDefault(); }, true); document.execCommand('copy'); alert('복사완료'); }) };
您可以將 function 插入錨點的 onclick 而不是 href。
按照代碼:
function myFunc() { let copyText = "LASTCHANCE1"; document.addEventListener('copy', function(e) { e.clipboardData.setData('text/plain', copyText); e.preventDefault(); }, true); document.execCommand('copy'); alert('복사완료'); }
<a href="#" onclick="myFunc();return false;" >Execute Func</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.