【发布时间】:2023-03-23 10:00:01
【问题描述】:
我正在尝试制作一个在您单击按钮时开始的倒计时。我似乎无法通过 onclick 调用该函数。从 onclick 调用带有字符串参数的函数的正确语法是什么?我试过了:
onclick="countDown(10, 'status')"
但这不起作用。有谁知道我做错了什么?
演示:
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = "Please wait for "+secs+" seconds";
if(secs<1){
clearTimeout(timer);
element.innerHTML = '<h2>Countdown Complete!</h2>';
element.innerHTML += '<a href="#"> Click here now</a>';
}
secs--;
var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
<div id="status"></div>
<form>
<input type="submit" name="go" onclick="countDown(5, 'status'); return false;">
</form>
【问题讨论】:
-
不起作用 - 有什么问题?
-
在演示中效果很好
标签: javascript