JQuery Traversal-ancestor parent () parents () Parentsuntil () (22)

Source: Internet
Author: User
Tags manual

Ancestors were fathers, grandfathers or great-grandfather, etc.

With JQuery, you can walk up the DOM tree to find the ancestors of the elements. walk up the DOM tree

These jquery methods are useful for traversing up the DOM tree: The parent () parents () Parentsuntil () jquery Parent () method

The parent () method returns the immediate parent element of the selected element.

The method will only traverse the DOM tree up one level.

The following example returns the immediate parent element for each <span> element: instance

$ (document). Ready (function () {
  $ ("span"). parent ();
});

Try the jQuery parents () method yourself

The parents () method returns all the ancestor elements of the selected element, all the way up to the root element of the document (

The following example returns all ancestors of all <span> elements: instance

$ (document). Ready (function () {
  $ ("span"). parents ();
});

Try it yourself.

You can also use optional parameters to filter the search for ancestor elements.

The following example returns all ancestors of all <span> elements, and it is a <ul> element: instance

$ (document). Ready (function () {
  $ ("span"). Parents ("ul");
});

Try the jQuery parentsuntil () method yourself

The Parentsuntil () method returns all ancestor elements between two given elements.

The following example returns all ancestor elements between <span> and <div> elements: instance

$ (document). Ready (function () {
  $ ("span"). Parentsuntil ("div");
});

Try the jQuery Traversal reference manual Yourself

For all the jquery traversal methods, please visit our jquery Traversal reference manual.

Ancestors were fathers, grandfathers or great-grandfather, etc.

With JQuery, you can walk up the DOM tree to find the ancestors of the elements. walk up the DOM tree

These jquery methods are useful for traversing up the DOM tree: The parent () parents () Parentsuntil () jquery Parent () method

The parent () method returns the immediate parent element of the selected element.

The method will only traverse the DOM tree up one level.

The following example returns the immediate parent element for each <span> element: instance

$ (document). Ready (function () {
  $ ("span"). parent ();
});

Try the jQuery parents () method yourself

The parents () method returns all the ancestor elements of the selected element, all the way up to the root element of the document (

The following example returns all ancestors of all <span> elements: instance

$ (document). Ready (function () {
  $ ("span"). parents ();
});

Try it yourself.

You can also use optional parameters to filter the search for ancestor elements.

The following example returns all ancestors of all <span> elements, and it is a <ul> element: instance

$ (document). Ready (function () {
  $ ("span"). Parents ("ul");
});

Try the jQuery parentsuntil () method yourself

The Parentsuntil () method returns all ancestor elements between two given elements.

The following example returns all ancestor elements between <span> and <div> elements: instance

$ (document). Ready (function () {
  $ ("span"). Parentsuntil ("div");
});

Try the jQuery Traversal reference manual Yourself

For all the jquery traversal methods, please visit our jquery Traversal reference manual.

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.