一个计数器,我想实现的功能是:点击“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>

解决方案 »

  1.   

    先隐藏<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(); 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>
      

  2.   

     可以用dom的  createElement  和 appendChild  来创建一个按钮并将插入 某个元素中 如:    var button =document.createElement("button");
        button.text="Stop count";
        button.onclick=function()
        {
           //可以在这里操作你的函数
        }
        然后插入到文档中  document.forms[0].appendChild(button);  还有一种方法就是 写好两个按钮 然后通过 display 来控制 按钮 
      

  3.   


    <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>楼主 这个意思?
      

  4.   

    <html>
    <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>