JS or jquery How to get parent, child, sibling elements (including ancestor, grandchild, etc.)

Source: Internet
Author: User

Native JavaScript methods:

var a = document.getElementById ("Dom");      Del_space (a); Clean space      var b = a.childnodes;//Get all the child nodes of A;      var c = a.parentnode;//get parent node of A;      var d = a.nextsibling;//Get the next sibling section of a Point      var e = a.previoussibling;//Get the previous sibling of a      var f = a.firstchild;//Get the first child node of a      var g = a.lastchild;//Get the last child of a Node

  

jquery Method:

Jquery.parent (expr)//To find a Father node, you can pass in expr for filtering, such as $ ("span"). Parent () or $ ("span"). Parent (". Class")jquery.parents (expr)//similar to jquery.parents (expr), but finds all ancestor elements, not limited to parent elementsJquery.children (expr)//returns all child nodes, this method only returns the immediate child node and does not return all descendant nodesjquery.contents ()//returns all content, including nodes and text, below. The difference between this method and children () is that, including blank text, it will be returned as a jquery object, and children () will only return the nodeJquery.prev ()//return to the previous sibling node, not all sibling nodesJquery.prevall ()//return all previous sibling nodesJquery.next ()//returns the next sibling node, not all sibling nodesJquery.nextall ()//return all subsequent sibling nodesjquery.siblings ()//back to sibling nodes, no pointsjquery.find (expr)//is completely different from jquery.filter (expr). Jquery.filter () is a partial filter from the initial set of JQuery objects, and the returned result of Jquery.find () does not have the contents of the initial collection, such as $ ("P"), Find ("span"), which is the start of the P element, equivalent to $ ("p Span ").

JS or jquery How to get parent, child, sibling elements (including ancestor, grandchild, etc.)

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.