How does jquery determine whether a specified child element exists
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!