【发布时间】:2021-12-10 03:48:48
【问题描述】:
我想创建一个倒计时按钮,它会在一定时间后显示隐藏的文本。 下面是5秒后下载文件的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://kit.fontawesome.com/e48d166edc.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
<title>Download Button With Timer</title>
</head>
<body>
<div class="download-container">
<a href="#" class="download-btn"> <i class="fas fa-cloud-download-alt "></i> Download Now</a>
<div class="countdown"></div>
</div>
<script type="text/javascript">
const downloadBtn = document.querySelector(".download-btn");
const countdown = document.querySelector(".countdown");
const pleaseWaitText = document.querySelector(".pleaseWait-text");
const manualDownloadText = document.querySelector(".manualDownload-text");
const manualDownloadLink = document.querySelector(".manualDownload-link");
var timeLeft = 5;
downloadBtn.addEventListener("click", () => {
downloadBtn.style.display = "none";
countdown.innerHTML = "Your download will start in <span>" + timeLeft + "</span> seconds." //for quick start of countdown
var downloadTimer = setInterval(function timeCount() {
timeLeft -= 1;
countdown.innerHTML = "Your download will start in <span>" + timeLeft + "</span> seconds.";
if(timeLeft <= 0){
clearInterval(downloadTimer);
pleaseWaitText.style.display = "block";
let download_href = "Testfile.rar"; //enter the downlodable file link here
window.location.href = download_href;
manualDownloadLink.href = download_href;
setTimeout(() => {
pleaseWaitText.style.display = "none";
manualDownloadText.style.display = "block"
}, 4000);
}
}, 1000);
});
</script>
</body>
</html>
现在我想把它改成一个按钮,它会在 5 分钟后显示一个文本,并且可以放在 WordPress 帖子中,但我不知道该怎么做。 请帮帮我。
【问题讨论】:
-
您正在尝试访问标记中不存在的元素,因此您遇到了错误。
-
你能告诉我更清楚吗?抱歉,我对代码 html 了解不多
-
在提供的代码中,您引用了
manualDownloadText或pleaseWaitText等元素,但这些元素不会出现在 HTML 中,因此您的 Javascript 代码中会出现错误。 -
谢谢@Professor Abronsius,但我想创建一个倒计时按钮以在几秒钟后显示文本。例如:“点击获取您的代码”,点击并等待几秒钟后,用户将获得他们的代码。
-
如果您要准确描述应该发生的事情可能会有所帮助?
标签: javascript html wordpress