jquery Hierarchy Selector Usage Analysis _jquery

Source: Internet
Author: User
Tags prev

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:
$ ("Parent->chilid");

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:
$ ("prev + next")

4. Adjacent sibling element selector

Used to select all sibling elements following an element, in the following syntax format:

Copy Code code as follows:
$ ("prev~siblings")

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.