This example describes the use of the Children () method in jquery. Share to everyone for your reference. The specific analysis is as follows:
This method gets a collection of elements that contain all the child elements of each element in the matching element collection.
You can filter the matched child elements through an optional expression.
Note: Find () finds all the child elements, and children () gets only the first level of child elements.
Syntax structure:
Copy Code code as follows:
$ (selector). Children (expr)
Parameter list:
| Parameters |
Describe |
| Expr |
Optional. An expression used to filter child elements |
Instance code:
Example one:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "Author" co ntent= "http://www.jb51.net/"/>
<title>children () function-cloud-Habitat community </title>
<style type= "Text/css" >
p{
border:1px solid Blue
}
. children{
border:1px solid green;
}
</style>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script& Gt
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (". Father"). Children (). css ("Height", "100px");
})
</script>
<body>
<div class= "Father" >
<div Class = "Children" >
<p> I am grandson p</p>
</div>
<p> I'm son p</p>
</d Iv>
<p> I am a brother p</p>
</body>
This method matches only one level of child elements.
Example two:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta name= "Author" content= "http://www.jb51.net/"/>
<title>children ()-cloud-dwelling community </title>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (". Father"). Children (". Children"). CSS ("Color", "red");
})
</script>
<body>
<div class= "Father" >
<div class= "Children" >
<p> I'm grandson P</p>
</div>
<p> I am the son p</p>
</div>
<p> I'm a brother p</p>
</body>
The above code sets the font color to red in a child element with a class attribute value of children.