简体   繁体   English

Jquery获取系列中最后一个元素的最后一行表

[英]Jquery Get last row of table within last element in a series

I have a series of slides based off of sections: 我有一系列基于部分的幻灯片:

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

I need to select grab the ID of the last row from the table in the last section of #first section. 我需要选择从#first部分的最后一部分中的表中获取最后一行的ID。

I'm using the following Jquery, getting "undefined" back...any ideas? 我正在使用以下Jquery,回到“未定义”......任何想法?

    var lastListItem = $('#first:last-child table>tbody>tr:last').attr("id");
    alert(lastListItem);
$('#first table:last tr:last')

or: 要么:

$('#first tr:last')

http://jsfiddle.net/hunter/QMzHH/ http://jsfiddle.net/hunter/QMzHH/

var lastListItem = $('#first section:last  table tr:last').attr("id");
var lastListItem = $("#first").find("section").last().find("tr").last().attr("id");

I prefer using [0].id instead of .attr("id") since its one less method call; 我更喜欢使用[0] .id而不是.attr(“id”),因为它少了一个方法调用; however, if you're not positive that you'll always have a table in that DOM position, attr is safer. 但是,如果你不肯定你在这个DOM位置总是有一个表,那么attr更安全。

.find(): 。找():

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. 获取当前匹配元素集中每个元素的后代,由选择器,jQuery对象或元素过滤。

​$("#first")​.find("tr:last").attr("id")

or more simply in this case: 或者更简单地说就是这种情况:

$("#first tr:last").attr("id")

EXAMPLE

The other answers work but the problem with your attempt is the fact that :last-child has to be applied to the child element ( section ), not the parent ( #first ). 其他答案有效但你尝试的问题是:last-child必须应用于子元素( section ),而不是父元素( #first )。 The following should work 以下应该有效

$('#first section:last-child table>tbody>tr:last').attr("id");

And could be simplified to 并且可以简化为

$('#first section:last-child tr:last-child').attr("id");

http://jsfiddle.net/e7mUD/1 http://jsfiddle.net/e7mUD/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM