jquery and JavaScript get parent node and child node

Source: Internet
Author: User

Parent node:

The code is as follows Copy Code

<table><tr><td>
<table><tr><td><span id= "E"/></td></tr></table>
</td></tr></table>

Gets the first table node of E

The code is as follows Copy Code
$ ("#e"). Parents ("Table:first") Yes.

Child nodes:

The code is as follows Copy Code

<div id= "abc" >
<div>a</div>
<div>b</div>
</div>


Get from parent node:
(1) Parentobj.firstchild: This method can be used if the node is the first child node of a known node (parentobj). This property can be recursively used, that is, support parentObj.firstChild.firstChild.firstChild ... The form so that you can get deeper nodes.
(2) Parentobj.lastchild: Obviously, this property is the last child node to get a known node (parentobj). As with FirstChild, it can also be used recursively.

Similar to JS in document.getElementById (' abc '). FirstChild like this.

Example

The code is as follows Copy Code

$ ("#abc >div:first-child")
$ ("#abc >div:last-child")


jquery gets more of the parent element methods, such as parent (), parents (), closest (), which can help you to find the parent element or node, let's go through one by one to explain:

Let me give you an example,


Our goal is to use the ID of item1 note A to the class for the Parent1 UL elements, there are several ways:

1. Parent ([expr])

Gets a collection of elements that contains the unique parent element of all matching elements.

You can use an optional expression to filter.

The code is as follows

1.$ (' #item1 '). Parent (). Parent ('. Parent1 ');

2.:p arent

Matches elements that contain child elements or text

The code is as follows

1.$ (' li:parent ');

3. Parents ([expr])

Gets a collection of elements that contain the ancestor elements of all matching elements (without the root element). You can filter by an optional expression.

The code is as follows

1.$ (' #items '). Parents ('. Parent1 ');

4. Closest ([expr])

Closest first checks whether the current element matches, and returns the element itself if it matches. If it does not match, look up the parent element, one level up, until you find the element that matches the selector. If nothing is found, an empty jquery object is returned.

The main differences between closest and parents are: 1, the former from the current element to match the search, the latter from the parent element to match the search, 2, the first step up until found the matching element stopped, the latter has been looking up until the root element, and then put these elements into a temporary collection, Filtering with the given selector expression, 3, which returns 0 or 1 elements, which may contain 0, 1, or more elements.

Closest is useful for handling event delegation.

1.$ (' #items1 '). Closest ('. Parent1 ');

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.