Get the App
SLTechnology News&Howtos  ›  Development  › 

How does jquery determine whether a specified child element exists

Shulou Source: shulou.com Published: 2022-06-01 11:36:52 11月14日 Update

This article mainly explains the "jquery how to determine the existence of the specified sub-element", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "jquery how to determine the existence of the specified sub-element" bar!

Judgment method: 1, use the "$(" parent element "). Has (" child element "). Length" statement, if the return value is 1, then specify the existence of the child element; 2, use the "$(" parent element "). Children (" child element "). Length" statement, if the return value is greater than or equal to 1, then specify the existence of the child element.

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Jquery determines whether the specified child element exists

Method 1: using has () method

Has () reduces the set of matching elements to a subset that has descendants that match the specified selector or DOM element.

$(document) .ready (function () {$("button") .click (function () {if ($("div"). Has ("span") .length) {console.log ("specify child element exists") } else {console.log ("specified child element does not exist")}}) })

Child element 1

Child element 2

Specify whether the child element span exists

Method 2: use children ()

The children () method returns all direct child elements of the selected element.

$(document) .ready (function () {$("button") .click (function () {if ($("div"). Children ("p") .length) {console.log ("specify child element exists") Console.log ($("div") .children ("p") .length);} else {console.log ("specified child element does not exist") Console.log ($("div") .length ("p") .length);}});})

Child element 1

Child element 2

Child element 3

Child element 4

Specify whether the child element exists

Thank you for your reading, the above is the content of "how jquery determines the existence of specified sub-elements". After the study of this article, I believe you have a deeper understanding of how jquery determines the existence of specified sub-elements, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Elements stators methods learning content sentences descendants subsets that is ideas situations tutorials articles more versions environment computers knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Microsoft macOS Apple