页面中有很多个 name=“a” 的标签,点击的时候怎么知道它是 name=“a”集合中的第几个标签?

解决方案 »

  1.   

    NAME相同的element 是一个数组的集合,所以都是有顺序的 ,    0,1,2,3.
      

  2.   

    IE 8 下测试通过
    <script type="text/javascript">
        
        function checkControlIndex(control){
        
            var allNameAControls = document.getElementsByName("a");
            
            for(var i=0;i<=allNameAControls.length-1;i++){
                if(control == allNameAControls[i]){
                    alert("第" + (i + 1) + "个控件");
                    break;
                }
            }
        }
            
        </script>
        
        <input name="a" value="This is a textbox with name &quot;a&quot;" style="width: 200px;" onclick="checkControlIndex(this);" />
        <br />
        <input type="button" name="a" value="This is a button with name &quot;a&quot;" onclick="checkControlIndex(this);" />
        <br />
        <input type="button" name="a" value="This is another button with name &quot;a&quot;"
            onclick="checkControlIndex(this);" />
        <br />
        <input type="radio" id="r1" name="a" onclick="checkControlIndex(this);" />
        <label for="r1">
            This is a radio button with name &quot;a&quot;</label>
        <br />
        <input type="checkbox" id="c1" name="a" onclick="checkControlIndex(this);" />
        <label for="c1">
            This is a radio button with name &quot;a&quot;</label>
      

  3.   

    那用jquery怎么找到指定的html标签