在td中找到第一个div

问题描述 投票:1回答:2

我正指向我表td中的第一个div,正如您在下面的jQuery代码中看到的那样。

$('table tr td div').css("position", "relative"); 

但是我遇到了一些问题所以我试过这个:

$('table tr td').parents('div:first').css({position:'relative'});

但是这个也不行。我的代码出了什么问题?

我的HTML结构是:

<table>
    <tr>
        <td><!-- all id for the div and image inside the td are dynamic -->
            <div><!-- Need to access this div -->
                <!-- content --->
            </div>
        </td>
    </tr>
</table>
javascript jquery
2个回答
4
投票

.parents()找到您所选择的所有父母。你想要的是一个孩子。

$('table tr td div:first').css("position", "relative"); 

这将为你做诀窍:)


您也可能只想选择精确的后代,在这种情况下您可以使用:

$('table tr td > div:first').css("position", "relative"); 

0
投票

如果要使用类'vx1'从TD中找到第一个DIV的出现,可以使用以下2种方法

$( "table tr td.vx1 > div:first" ).css( "width", "50" );

如果要使用类'vx1'从TH中找到第一个DIV的出现,可以使用以下2种方法

$( "table tr th.vx1 > div:first" ).css( "width", "50" );
© www.soinside.com 2019 - 2024. All rights reserved.