使用javascript闭包获取table标签中tr的索引

<!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" xml:lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <title>Document</title>
    <script type="text/javascript">
$(function() {
    var $tabObj;
    $tabObj = $('table').find('tr');
    for (var i = 0, l = $tabObj.length; i < l; i++) {
        (function(i) {
            $tabObj.eq(i).bind('click', function(){
                alert($tabObj.eq(i).attr('class'));
                alert(i);
            })
        })(i);
    };
})
</script>

</head>

<body>
    <table>
        <tr class="item1"><td>11111</td></tr>
        <tr class="item2"><td>22222</td></tr>
        <tr class="item3"><td>33333</td></tr>
        <tr class="item4"><td>44444</td></tr>
        <tr class="item5"><td>55555</td></tr>
        <tr class="item6"><td>66666</td></tr>
    </table>
</body>

</html>

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章:

  • 2022-01-09
  • 2021-11-10
  • 2022-12-23
  • 2021-12-04
  • 2022-01-02
  • 2022-01-12
  • 2022-02-05
  • 2021-12-01
猜你喜欢
  • 2022-01-16
  • 2022-01-02
  • 2021-10-31
  • 2021-07-22
  • 2022-12-23
  • 2022-01-26
  • 2021-06-20
相关资源
相似解决方案