1 回答
TA贡献1898条经验 获得超8个赞
使用不同 ID 的简单解决方案
function download(btn) {
id = Number(btn.id.slice("downloadbutton".length+1))
var timeleft = 3;
var downloadTimer = setInterval(function function1() {
document.getElementById('download' + id).style.display = "none";
document.getElementById("timer" + id).innerHTML = "Wait " + timeleft + "";
if (timeleft <= 0) {
clearInterval(downloadTimer);
document.getElementById("timer" + id).innerHTML = ""
//window.open(document.querySelector('#downloadbutton' + id + ' a').href, '_blank');
document.getElementById('download' + id).style.display = "";
}
timeleft -= 1;
}, 1000);
}
.container {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
#download_button {
border: none;
margin-top: 0px;
padding: 10px;
width: 200px;
font-family: "montserrat", sans-serif;
text-transform: uppercase;
border-radius: 6px;
cursor: pointer;
color: #fff;
font-size: 16px;
transition: 0.4s;
line-height: 28px;
outline: none;
}
.button-1 {
background: #f12711;
}
.button-2 {
background: #0575E6;
}
.button-3 {
background: #fe8c00;
}
#download_button:hover {
background: #000000;
}
.title {
font-weight: bold;
}
<div id="downloadbutton1" style="text-align: center;">
<button id="download_button1" class="button-1" onclick="download(this)">
<div class="title">Document Title 1</div>
<div id="download1">DOWNLOAD</div>
<div id="timer1"></div>
</button>
</div>
<br>
<div id="downloadbutton2" style="text-align: center;">
<button id="download_button2" class="button-2" onclick="download(this)">
<div class="title">Document Title 2</div>
<div id="download2">DOWNLOAD</div>
<div id="timer2"></div>
</button>
</div>
<br>
<div id="downloadbutton3" style="text-align: center;">
<button id="download_button3" class="button-3" onclick="download(this)">
<div class="title">Document Title 3</div>
<div id="download3">DOWNLOAD</div>
<div id="timer3"></div>
</button>
</div>
添加回答
举报