How do you know if the current element is not the first-child?
It should work with $(this)
, for example:
$("li").click(function(e) { if (/* $(this) is not the first-child */) { /* do something */ } });
Use the :not(selector) Selector Not to Select the First Child in CSS. We can use the :not(selector) selector to select every other element that is not the selected element. So, we can use the selector not to select the first child in CSS. We can use :first-child as the selector in the :not(selector) selector.
ul:not(:first-child) means literally "any ul element that is not first child of its parent", so it won't match even the 1st ul if it's preceded by another element ( p , heading etc.). On the contrary, ul:not(:first-of-type) means "any ul element except the 1st ul in the container".
The first-child is a pseudo class in CSS which represents the first element among a group of sibling elements. The :first-child Selector is used to target the first child element of it's parent for styling. Example: HTML.
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
You can do this just to test an element if it's the first child: $(this).is(':first-child')
. Other selectors like :last-child
would work too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With