質問

Is there a jQuery parent selector that traverses up the DOM until the first match is found?

Eg:

<tr>
    <td>
        <div id="foo">hello!</div>
    </td>
</tr>

to find the row from the div I am using:

$('#foo').parent().parent();

It feels like I should be able to write something like

$('#foo').firstParent('tr');

but I can't find any such function in the docs.

役に立ちましたか?

解決

You can use .closest() for this:

$('#foo').closest('tr');

If it helps, there's a category specifically for this to narrow your future searches to: Tree Traversal

他のヒント

$(element).parents('TR:first');

Edit: Or what Nick said below - forgot about that sucker.

$('#foo').parent('tr')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top