[英]Jquery Get last row of table within last element in a series
我有一系列基於部分的幻燈片:
<div id="slides">
<section id="first">
<section>
<table>
<thead>
</thead>
<tbody>
<tr id="somethingUnique">
...
</tr>
</tbody>
</table>
</section>
<section>
<table>
<thead>
</thead>
<tbody>
<tr id="somethingUnique">
...
</tr>
</tbody>
</table>
</section>
...
</section>
</div>
我需要選擇從#first部分的最后一部分中的表中獲取最后一行的ID。
我正在使用以下Jquery,回到“未定義”......任何想法?
var lastListItem = $('#first:last-child table>tbody>tr:last').attr("id");
alert(lastListItem);
var lastListItem = $('#first section:last table tr:last').attr("id");
var lastListItem = $("#first").find("section").last().find("tr").last().attr("id");
我更喜歡使用[0] .id而不是.attr(“id”),因為它少了一個方法調用; 但是,如果你不肯定你在這個DOM位置總是有一個表,那么attr更安全。
其他答案有效但你嘗試的問題是:last-child
必須應用於子元素( section
),而不是父元素( #first
)。 以下應該有效
$('#first section:last-child table>tbody>tr:last').attr("id");
並且可以簡化為
$('#first section:last-child tr:last-child').attr("id");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.