【问题标题】:Change Button Text after click back单击返回后更改按钮文本
【发布时间】:2021-06-29 21:31:32
【问题描述】:

尊敬的 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>

【问题讨论】:

    标签: javascript html web button


    【解决方案1】:

    您可以在此处使用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
    })
    

    【讨论】:

    • 非常感谢!我直接尝试过,它确实有效:)
    • 是的,会的。我只需要等待 3 分钟,以便 StackOverflow 允许它;D
    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 2016-02-29
    • 2022-12-04
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多