简体   繁体   English

使用JQuery选择器选择元素的父级

[英]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 . 我想在我的TR中找到TD的计数,其中一个TD符合我的标准。 Here is the skeleton of my html structure, 这是我的html结构的骨架,

<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: 我的JQuery选择器代码是:

$('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. 你没有div的class属性。

Change 1st row to this: 将第1行更改为:

<div class="pager">

There is no :parent selector a jQuery selectors are evaluated right-to left. 没有:parent选择器从右到左评估jQuery选择器。 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() 假设您没有其他具有ID属性的TD,您可以使用td[id]检查TD上是否存在ID属性,并使用:has()定位其父行:has()

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

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

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

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