In an HTML document, each element is always in a location on the DOM node tree, and there is always a hierarchical relationship between elements in the document hierarchy, such as the relationship between parent and child.
1. Child element Selector
Used to find all child elements under this parent element under a given parent element, syntax format:
Copy Code code as follows:
2. Descendant element Selector
Used to match all descendant elements under a given ancestor element, in syntax format:
Copy Code code as follows:
$ ("ancestor descendant");
3. Adjacent sibling element selector
Used to match all next elements immediately after the Prev element, in syntax format:
Copy Code code as follows:
4. Adjacent sibling element selector
Used to select all sibling elements following an element, in the following syntax format:
Copy Code code as follows:
General Examples:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" src= "Jquery-1.7.min.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("form input"). CSS ("Color", "red"); Set the font color for the descendant INPUT element of a FORM element
$ ("Div>div"). CSS ("Background", "#FCF"); Set the background color for child elements div1 and Div2 under Maindiv
$ ("Div~input"). CSS ("Border", "2px solid Blue"); To set a border for all input elements that follow a DIV element
$ ("Div+input"). CSS ("Border", "2px solid Red"); To set a border on the input element immediately after the DIV element
});
</script>
<title> level selector </title>
<body>
<form id= "Form1" >
The descendant input elements of the <label>form element are: input1, Input2, input3</label>
<input type= "text" id= "input1" value= "TextBox 1"/>
<div id= "Maindiv" >
<div id= the child element of the "DIV1" >maindiv: ID is div1</div>
<div id= the child element of the "Div2" >maindiv: ID is div2</div>
</div>
<input type= "text" id= "Input2" value= "TextBox 2"/>
<input type= "text" id= "input3" value= text box 3 "/><br/>
All the child elements of the <label>maindiv are: Div1, div2</label>
</form>
</body>
The effect is shown in the following illustration:
I hope this article will help you with your jquery programming.