jQuery: find() children until a certain threshold element is encountered

前端 未结 4 1318
不思量自难忘°
不思量自难忘° 2021-02-19 17:59

I have a nested table structure like

   
4条回答
  •  你的背包
    2021-02-19 18:36

    Update: Let's have another take at this.

    Basically, you want to match all the elements that are descendants of #first and that are not children of

elements nested more than one level deep under #first.

(I'm not sure about that last under #first part, but implementing it allows us to support

elements above #first in the ancestor chain.)

Technically, the following selector alone should fulfill your requirements:

var inputs = $("#first td:not(#first td td) > input");

If this does not work in your browser (Sizzle should be up to the task I think, but complex selectors like :not() are always tricky), you can delegate the processing to jQuery methods:

var inputs = $("#first td").not("#first td td").children("input");

Original answer follows:

You can use not() to exclude elements that have more than one

ancestor:

var firstLevelCells = $("#first").find("input").not("td td input");

提交回复
热议问题