<body> 
<span id="spanTitle0">sd </span> <span id="spanTitle1">sdf </span> 
<body> 
<script language="javascript"> 
var letterName; 
var currentLetter; 
for(var i=0;i <2;i++) 

    letterName = "spanTitle"+i; 
    currentLetter = document.getElementById(letterName); 
currentLetter.onclick=function(){alert(this.id)};
/*
    if (window.attachEvent) 
    { 
      currentLetter.attachEvent("onclick",function(){alert(currentLetter.id)}); //这个地方是错的
    } 
    else 
    { 
        currentLetter.addEventListener("click", "alert(currentLetter.innerHTML);"); 
    } 
*/

</script>囧 attachEvent在研究下...

解决方案 »

  1.   

    也许可以用闭包吧  只是也许  我对闭包不大懂
    http://topic.csdn.net/u/20081005/22/788f5d0d-4cc1-46b8-80dd-39b52faa0f5a.html
    这个连接的4楼的好象就是在这中情况下用的闭包   (个人认为了,我不大懂了)
      

  2.   


    currentLetter.attachEvent("onclick",function(currentLetter){return function(){alert(currentLetter.id)}}(currentLetter)); 这样的写法我都忘记了丢人啊
    是要运行一个函数把当前的currentLetter传进去“绑定”住@#$%^&*
    汗 意会。
      

  3.   

    以前也碰到了类似的问题,最后还是转换了一下思想才解决
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    .btn{
        width:20px;
        height:18px;
        margin:0 2px;
        background:#ccc;
        color:#333;
        border:1px solid #999;
    }
    .btnCurrent{
        width:20px;
        height:18px;
        margin:0 2px;
        background:#fff;
        color:#333;
        border:1px solid #999;
    }
    #div{
        border:1px gray solid;
        padding:10px;
    }
    </style>
    </head>
    <script type="text/javascript">
    function test(){
    try{
        var total=10;
        var currentId=5;
        callback=function(num){alert(num);}
        var div=document.createElement("div");
        div.setAttribute("id","div");
        document.body.appendChild(div);
        for(i=1;i<total;i++)
        {
            if(i==currentId){
                var btn=document.createElement("input");
                btn.setAttribute("id","btn_"+i);
                btn.className="btnCurrent";
                btn.setAttribute("type","button");
                btn.setAttribute("value",i);
                div.appendChild(btn);
            }else{
                var m=Math.random();
                var btn=document.createElement("input");
                btn.setAttribute("id","btn_"+m);
                btn.className="btn";
                btn.setAttribute("type","button");
                btn.setAttribute("value",i);
                btn.onclick=function(e){
                    e=window.event||e;
                    tar=e.srcElement||e.target;
                    alert(tar.value);
                }
                div.appendChild(btn);
            }
        }
    }catch(e){alert(e)}
    }
    </script>
    <body>
    <a href="javascript:test();">test</a>
    </body>
    </html>上面这段代码如果是引用i的话就得不到想要的效果了,因为i是动态开辟的,最后结果都是10可能是ACTIVE CODE开辟的是一段动态的内存地址,指针会往下走
      

  4.   

    多谢各位
    to 1楼 中文的解释我都看不懂,英文的。to 8楼 虽然你写了很多  其实就是 event.srcElement or e.target  ,我只是想知道闭包的用法 你写的我也会!~~~