var i=0;function addalert()
{
i++;
alert('警告'+i);
}<input type="button" value="点击" onclick="addalert();" />

解决方案 »

  1.   

    定义个常量
    var count=0;<input type="button" onclick="alert(++count);" />
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
    <META HTTP-EQUIV="Expires" CONTENT="0"> 
    <title>please insert title</title>
    </head>
    <body>
    <script>
    var i=0;
    function doalert()
    {
       i++;
       alert("警告"+i);
    }
    </script>
    <input type="button" name="testbtn" value="测试"  onclick="doalert()" />
    </body>
    </html>
      

  3.   


    <script type="text/jscript">
       //定义全局变量i;
       var i=0;
       function doCalc(){
          alert("第"+(++i)+"次警告");
       }
    </script>
    <input type="button" id="calc" value="数数" onclick="doCalc()">
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
     </HEAD> <BODY>
        <input type="button" name="btn" id="btn" value="单击后就加一"/>
    <script>
    var count = 0;
         var btn = document.getElementById("btn");
     btn.onclick = function() {
        alert(++count)
     }
    </script>
     </BODY>
    </HTML>
      

  5.   

    恩,貌似都是全局变量的。或者你也可以这样:<script>
        var hitCount = function(obj){
            var hc = obj.getAttribute("hitcount");
            hc = (hc>0)?++hc:1;
            obj.setAttribute("hitcount",hc);
            alert(hc)
        }
    </script><input type="button" onclick="hitCount(this);" value="单击后就加一"/>
      

  6.   

    又或者,可以用一个静态变量:
    <script>
        var oButton = (function(){
            var hc = 0;
            return {
                hitCount:function(){
                    alert(++hc);
                }
            }
        })()
    </script><input type="button" onclick="oButton.hitCount();" value="单击后就加一"/>
      

  7.   

    当然上面的静态变量其实是不够安全的,因为在FF中可以通过eval将它解闭包,如:
    <script>
        var oButton = (function(){
            var hc = 0;
            return {
                hitCount:function(){
                    alert(++hc);
                }
            }
        })()    oButton.hitCount();
        eval('hc=100',oButton.hitCount);
        oButton.hitCount();
    </script>可以考虑将这个静态变量写到prototype中,参见BL的文章:
    http://www.never-online.net/blog/article.asp?id=225是不是有点跑题? ^_^
      

  8.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> 
    <META HTTP-EQUIV="Expires" CONTENT="0"> 
    <title>please insert title</title>
    </head>
    <body>
    <script>
    var i=1;
    function doalert()
    {
       i++;
       var j = i%2+1;
       alert("警告"+j);
    }
    </script>
    <input type="button" name="testbtn" value="测试"  onclick="doalert()" />
    </body>
    </html>
      

  9.   

    <script language="javascript">
    var a=1;
    function jinggao1()
    {
        alert("1");
        a=2;
    }
    function jinggao2()
    {
      alert("2");
      a=1;
    }
    function jinggao()
    {
      if (a==1)
      {
       jinggao1()
      }
      else if (a==2)
      {
       jinggao2()
      }
    }
    </script>
     <BODY>
     <INPUT TYPE="button" onclick="jinggao()" value="确定">
     </BODY>大家辛苦了。
    憋了好几天,还是自己写出来了。呵呵。不好意思,我嘴笨,其实我想要的就是上边的效果。
    终于明白if() else if() 跟if() else 跟if() if()的区别了,希望大家体会一下,有趣的很,呵呵