var startat = 5;
function disableRegBtn(bdisable){
document.getElementById('regBtn').disabled=bdisable;
}
function showTime(){
if(startat == 0){
clearInterval(timeId);
document.getElementById("p1").innerHTML = "please register now";
disableRegBtn("enabled");
return;
}
document.getElementById("p1").innerHTML = "please register in " + startat + " second(s)";
startat--;
}
window.onload=function(){
document.getElementById('taid').readOnly=true;
disableRegBtn(true);
timeId=setInterval("showTime()",1000);
}
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea> <p id="p1"><input id="regBtn" type="button" value="register" /></p>
我想让人们在提交注册前至少5秒钟阅读注册协议,当5秒钟结束时,please register now
可以显示在网页中,如何使<input id="regBtn" type="button" value="register" />
工作?
在showTime
函数中对disableRegBtn()
做了一些尝试。
disableRegBtn(false)
disableRegBtn("false")
disableRegBtn("disabled")
disableRegBtn("enabled")
它们都无法生效。
您不需要再次创建按钮,因为它已经在DOM中了。
您可以使用变量来保存元素,以便在需要时使用它们。这将确保您的代码更加干净和健壮。
当计数器为0
时,您可以传递flase
,而不是传递启用的。我还建议您在处理纯文本内容时使用textContent
而不是innerHTML
,因为它更快速、更可预测。
var startat = 5;
var buttonEl = document.getElementById('regBtn');
var pEl = document.getElementById("p1");
function disableRegBtn(bdisable){
buttonEl.disabled = bdisable;
pEl.textContent = '';
pEl.prepend(buttonEl);
}
function showTime(){
if(startat == 0){
clearInterval(timeId);
pEl.textContent = "please register now";
disableRegBtn(false);
return;
}
pEl.textContent = "please register in " + startat + " second(s)";
startat--;
}
window.onload=function(){
document.getElementById('taid').readOnly=true;
disableRegBtn(true);
timeId=setInterval("showTime()",1000);
}
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea> <p id="p1"><input id="regBtn" type="button" value="register" /></p>
在执行document.getElementById("p1").innerHTML = "please register in...
时删除的按钮只需在超时后再次添加按钮
var startat = 5;
function disableRegBtn(bdisable) {
document.getElementById('regBtn').disabled = bdisable;
}
function showTime() {
if (startat == 0) {
clearInterval(timeId);
document.getElementById("p1").innerHTML = '<input id="regBtn" type="button" value="register" />';
return;
}
document.getElementById("p1").innerHTML = "please register in " + startat + " second(s)";
startat--;
}
window.onload = function() {
document.getElementById('taid').readOnly = true;
disableRegBtn(true);
timeId = setInterval("showTime()", 1000);
}
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea>
<p id="p1"><input id="regBtn" type="button" value="register" /></p>