Comment savoir si l’élément en cours n’est pas le premier enfant?
Cela devrait fonctionner avec $(this)
, par exemple:
$("li").click(function(e) { if (/* $(this) is not the first-child */) { /* do something */ } });
Vous pouvez le faire uniquement pour tester un élément s’il s’agit du premier enfant: $(this).is(':first-child')
. D’autres sélecteurs tels que :last-child
fonctionneraient aussi.
Vous pouvez simplement demander sa position .index()
(par rapport à ses frères et sœurs).
$(this).index(); // returns a zero based index position
Pour tous les #thing
qui ne sont pas le premier enfant:
$('#thing li:not(:first-child)')
Si vous souhaitez tout sélectionner sauf le premier enfant, procédez comme suit:
$('#some-parent').children().not(':first')
En outre, vous pouvez vérifier si $(this).prev().length
est défini, comme dans:
if (!$(this).prev().length){ //This means $(this is the first child //do stuff }
Vérifier l’index
$(this).index() == 0 // is first
Restez simple, utilisez le DOM
$("li").click(function(e) { if (this.previousSibling != null) { /* do something */ } });
jQuery .not ()
$(this).not(':first');
$("li").click(function(e) { if ($(this).index != 0 ) { /* do something */ } });