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

How to determine if there are child elements in jquery

coldplay.xixi
Release: 2020-11-30 17:21:00
Original
3223 people have browsed it

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子标记   
}
Copy after login

2. Select the element under a specific id Specific id sub-element

$("#form" ).children( "#t" )
Copy after login

3. Select the sub-element under the specific id element

$("ul#u>li:nth-child(2)" )
Copy after login

4. Determine whether an element exists

if ($( "#myId" ).length>0){ 
   //存在    
}
Copy after login

5. Determine whether an element exists Whether there are child elements for each element

if ($( "#myId" ).children().length>0){ 
   //存在    
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template