javascript闭包获取table中tr的索引 分类: JavaScript 2015-05-04 15:10 793人阅读 评论(0) 收藏

使用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>

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

原文地址:https://www.cnblogs.com/yisuowushinian/p/4715605.html