Jquery learning notes (1): Hierarchy Selector

Source: Internet
Author: User

Describes in detail the differences between ancestor descendant and parent> child in the jquery hierarchy selector.
Parent> child: match all child elements based on the parent element. hierarchies are parent-child relationships.

Ancestor descendant: matches all descendant elements based on the ancestor element. The hierarchy is the ancestor and descendant.

Write code and perform tests to better differentiate the differences between the two:
<Div id = "first"> 1
<Span> 1.1 </span>
<Span> 1.2 </span>
<Div> 1.3
<Span> 1.3.1 </span>
</Div>
</Div>
<SCRIPT type = "text/JavaScript" src = "scripts/jquery-1.4.1.min.js">
</SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Function (){
$ ("# First> span" ).css ("color", "Red ");
});
</SCRIPT>
After running:


1.3.1 The text color is not red, because parent> child is a parent-child relationship;
If you change the selector:
$ ("# First span" ).css ("color", "Red ");
After running, you will find:

The text color of 1.3.1 is also red, because the ancestor descendant hierarchy is the ancestor and descendant. That is, all span tags under the element with ID "first", both the child and the child, will become red.

 

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.