$(この)を組み合わせる方法はnth-child?
-
12-11-2019 - |
質問
私は。それぞれの反復の中央にあり、それぞれの子供のために2番目または3番目の子供を呼び出したい。
alert($(this + ' :nth-child(2)').attr('id'));
.
私が考えることができる私の唯一の選択肢このようなひどいgoofyです:
$(this).children(':first').next().attr('id', 'ddParam' + newCount);
$(this).children(':first').next().next().attr('id', 'txt' + newCount);
$(this).children(':first').next().next().next().attr('id'...
. 解決
あなたが必要なものは context 。コンテキストでは、セレクタはコンテキストの子である要素(この場合はthis
)を探すだけです。
$(':nth-child(2)', this).attr('id');
.
これは基本的に:
$(this).find(':nth-child(2)').attr('id');
.
すべての子孫ではなく直接の子供だけが必要な場合は、.children()
を使用する必要があります。
$(this).children(':nth-child(2)').attr('id');
. 所属していません StackOverflow