简体   繁体   中英

Selecting Parent of Element Using JQuery selectors

I want to find the count of TD inside my TR where one of its TD matches my criteria . Here is the skeleton of my html structure,

<div class="pager">
        <table class="pager_table">
            <tbody>
                <tr>
                    <td></td>
                    <td id ="sometextPager_left"></td>
                    <td></td>
                ...................
        </table>
    </div>

And my JQuery selector code is:

$('div.pager > table.pager-table td[id$="Pager_left"] :parent tr > td').length

It seems my selector is not working,

Thanks

尝试使用:has()选择器,

$('div.pager > table.pager-table tr:has(td[id$="Pager_left"]) > td').length

You don't have class attribute on div.

Change 1st row to this:

<div class="pager">

There is no :parent selector a jQuery selectors are evaluated right-to left. Having a parent selector would require right-to-left evaluation.

Assuming you have no other TDs with ID attributes, you can simply check for the existence of an ID attribute on a TD with td[id] and target its parent row using :has()

$('.pager > .pager_table tr:has(td[id]) > td').length // returns 3

JSFiddle: http://jsfiddle.net/TrueBlueAussie/ghLcyp54/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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