Home  >  Article  >  Web Front-end  >  How to determine if there are child elements in jquery

How to determine if there are child elements in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-19 16:17:393209browse

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" )...].

How to determine if there are child elements in jquery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn