Home > Article > Web Front-end > How to determine if there are child elements in jquery
Jquery method to determine whether there are sub-elements: 1. To determine whether a specific sub-element exists, the code is [if ($( "#specialId:has(img)" ).length==0)]; 2 , select a specific id sub-element under a specific id element, the code is [$("#form" )...].
The operating environment of this tutorial: Windows 7 system, jquery version 3.3.1. This method is suitable for all brands of computers.
Jquery method to determine whether there are sub-elements:
1. Determine whether a specific sub-element exists
if ($( "#specialId:has(img)" ).length==0) { // 一级子元素 // 没有img子标记 } else { // 有img子标记 }
2. Select the element under a specific id Specific id sub-element
$("#form" ).children( "#t" )
3. Select the sub-element under the specific id element
$("ul#u>li:nth-child(2)" )
4. Determine whether an element exists
if ($( "#myId" ).length>0){ //存在 }
5. Determine whether an element exists Whether there are child elements for each element
if ($( "#myId" ).children().length>0){ //存在 }
Related free learning recommendations:JavaScript(video)
The above is the detailed content of How to determine if there are child elements in jquery. For more information, please follow other related articles on the PHP Chinese website!