本帖最后由 h123hu 于 2012-09-11 20:26:47 编辑

解决方案 »

  1.   


    function click_on(obj,i){
    obj.onclick = function(){
    ///写你的代码,这个地方i就代表你点击的是第几个标签
    };
    }
    var aaa = document.getElementsByClassName("t");//这个是按照class取值的,但是这个getElementsByClassName需要自己定义,这个很好找的
    var aaa = document.getElementsByTagName("ul")[0].getElementsByTagName("li");
    //取出第一个ul标签,在子标签中,查找li标签。然后click_on函数中的i值,就是你点击的第几个标签
    for(var i=0;i<aaa.length;i++){
    click_on(aaa[i],i);
    }
    试试这样~~
      

  2.   

    1:直接绑定事件,参数this则指向当前绑定事件的对象
    <div class="t" onclick="fun(this)" style="width:100px; height:100px; background-color:#000; float:left;"></div>2:遍历绑定
    <div class="t" style="width:100px; height:100px; background-color:#000; float:left;"></div>
    <div class="t" style="width:100px; height:100px; background-color:#fff; float:left;"></div><script type="text/javascript">
    var o=document.getElementsByTagName("div");
        for(i=0;i<o.length;i++){
            o[i].onclick=function(){
                alert(this.innerHTML);//this就是当前被点击对象
            }
        }
    </script>
      

  3.   

    可以考虑用index()方法。
    第一个问题$("div.t").click(function(){
    alert($("div.t").index(this));
    })$("div.t").index(this)获得的就是所点击的div在$("div.t")这个对象集合中的位置,从0开始。
    第二个问题也差不多$("ul.t > li").click(function(){
    alert($("ul.t > li").index(this));
    })
      

  4.   

    用jquery的话最简单
    $(".t>li").click里面的this就是被点击的对象
      

  5.   

    <ul class="t">
        <li style="width:100px; height:100px; background-color:#000; float:left;"></li>
        <li style="width:100px; height:100px; background-color:#bf0000; float:left;"></li>
        <li style="width:100px; height:100px; background-color:#fc0; float:left;"></li>
    </ul>
    <script type="text/javascript">
    //这两个问题其实是同一个问题,如果不想使用上面的this方法,可以考虑闭包
        var li=document.getElementsByTagName("li");
        for(var i=0;i<li.length;i++)
        {
            (function(n){
                li[n].onclick=function(){
                    alert(n);
                }
            })(i)
        }
    </script>