一个计数器,我想实现的功能是:点击“start count!”后,开始计数的同时 生成一个“Stop count!”按钮,点击此“Stop count!”可以停止计数,我是新手,不知道怎么实现,求帮忙,原有代码如下
<html><head>
<script type="text/javascript">
var c=0
var tfunction timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}function stopCount()
{
clearTimeout(t)
}
</script>
</head><body>
<form>
<input type="button" value="Start count!" onClick="timedCount();disabled=true">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount();disabled=true">
</form>
</body></html>
<html><head>
<script type="text/javascript">
var c=0
var tfunction timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}function stopCount()
{
clearTimeout(t)
}
</script>
</head><body>
<form>
<input type="button" value="Start count!" onClick="timedCount();disabled=true">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount();disabled=true">
</form>
</body></html>
<script type="text/javascript">
var c=0
var tfunction timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}function stopCount()
{
clearTimeout(t)
}
</script>
</head><body>
<form>
<input type="button" value="Start count!" onClick="timedCount(); bs.style.display=''; disabled=true">
<input type="text" id="txt">
<input type="button" style="display:none" id="bs" value="Stop count!" onClick="stopCount();disabled=true">
</form>
</body></html>
button.text="Stop count";
button.onclick=function()
{
//可以在这里操作你的函数
}
然后插入到文档中 document.forms[0].appendChild(button); 还有一种方法就是 写好两个按钮 然后通过 display 来控制 按钮
<input type="text" id="txt" value="0">
<button id="btn">start</button>
<script>
function $(o){return document.getElementById(o)}
$('btn').onclick = function(){
if(this.innerHTML == 'start'){
this.innerHTML = 'stop...';
t = setInterval('$("txt").value = +$("txt").value+1',100);
}else{
this.innerHTML = 'start';
clearInterval(t)
}
}
</script>楼主 这个意思?
<head></head>
<body>
<form>
<div><input type="text" id="textbox" value="0" /><div>
<input type="button" id="start" value="Start count" />
<input type="button" id="stop" value="Stop count" />
</form>
<script type="text/javascript">
var c = 0,
timer,
textbox = document.getElementById('textbox'),
stop_btn = document.getElementById('stop');
textbox.value = c;
document.getElementById('start').onclick = function(){
stop_btn.style.display = 'inline';
timer = setInterval(count, 1000);
}
stop_btn.onclick = function(){
stop_btn.style.display = 'none';
clearInterval(timer);
}
function count(){
c += 1;
textbox.value = c;
}
</script>
</body>
</html>