jQuery - 最初の子

jQuery –最初の子と最後の子のセレクターの例

:first-childは、親の最初の子であるすべての要素を選択するために使用されます。これは、:nth-​​child(1)の省略形です。

  1. $(‘li:first-child’) – selects all elements matched by

  2. that are the first child of their parent.

  3. $(tr:first-child’) – selects all elements matched by that are the first child of their parent.

:last-childは、親の最後の子であるすべての要素を選択するために使用されます。

  1. $(‘li:last-child’) – selects all elements matched by

  2. that are the last child of their parent.

  3. $(tr:last-child’) – selects all elements matched by that are the last child of their parent.

jQueryの例

最初の子関数と最後の子関数を使用して「li」背景色を動的に追加する簡単な例を示します。



jQuery first child and last child example






jQuery first child and last child example

  • li #1
  • li #2
  • li #3
  • li #4
  • li #5