javascript閉包擷取table中tr的索引,tabletr
使用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>