日期:2014-05-16  浏览次数:20271 次

jquery如何获得表中最后一个tr的id呢?
hi:

  各位,请教一下:通过jquery如何获得表中最后一个tr的id呢?我试了一下能获得tr的text

如下:var id = $("#tcontent").find("tr:last").children().text();

但是通过这种方式var id = $("#tcontent").find("tr:last").children().attr("id");

就获取不到ID,请大家帮忙看看是什么原因,多谢了。。


------解决方案--------------------
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var trId = $("#tcontent").find("tr").last ().attr("id");
alert(trId);
});
</script>
</head>
<table id="tcontent">
<tr>
<td>
<input type="text" id="" value=""/>
</td>
</tr>
<tr>
<td>
<input type="text" id="" value=""/>
</td>
</tr>
<tr>
<td>
<input type="text" id="" value=""/>
</td>
</tr>
<tr>
<td>
<input type="text" id="" value=""/>
</td>
</tr>
<tr>
<td>
<input type="text" id="" value=""/>
</td>
</tr>
<tr id="lastId">
<td>
<input type="text" id="" value=""/>
</td>
</tr>

</table>
</html>
------解决方案--------------------
JScript code

var id = $("#tcontent").find("tr:last").attr("id");

------解决方案--------------------
探讨
$("#tcontent").find("tr:last").children().attr("id");
你都说了是获取最后一个TR的ID 那你还children个毛啊