[英]Change Button Text after click back
親愛的 StackOverflow 社區,
我有這個代碼來將 URL 復制到剪貼板。 問題是,點擊后它停留在“kopiert”上。 我想讓它變回來。 我知道這個問題被問了 100 次,而且網上有代碼。 我的問題是,我對編程知之甚少,並在網上嘗試了代碼。 無法實施。 我真的需要幫助。 我真的很感謝任何人。
提前致謝。
代碼:
<div class="background">
<center>
<button class="clipboard">Link kopieren</button>
</center>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var $temp = $("<input>");
var $url = $(location).attr('href');
$('.clipboard').on('click', function() {
$("body").append($temp);
$temp.val($url).select();
document.execCommand("copy");
$temp.remove();
$("button").text("kopiert!");
})
</script>
您可以在此處使用setTimeout()
來運行延遲一定時間的函數。 例如,我們可以將您的 JavaScript 更新為:
var $temp = $("<input>");
var $url = $(location).attr('href');
$('.clipboard').on('click', function() {
const originalText = $("button").text();
$("body").append($temp);
$temp.val($url).select();
document.execCommand("copy");
$temp.remove();
$("button").text("kopiert!");
// Run something in 2.5 seconds to revert back to the button's text
setTimeout(function() {
$("button").text(originalText);
}, 2500); // 2.5 seconds
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.