Using jquery to select Traversal functions for complex HTML

Source: Internet
Author: User
Tags object functions new set

10 very useful traversal functions in jquery

With jquery, you can easily select HTML elements. But sometimes, when the HTML structure is more complex, refining the elements we choose is a hassle. In this tutorial, we'll explore 10 ways to refine and extend the set we're going to be working on.

Html

First, let's take a look at the simple page shown in the following illustration, which we will select from this tutorial.

Div.container is the parcel element;

Div.photo, Div.title and div.rating are direct children of Div.container;

Each div.star is a child of the div.rating;

When Div.satr's class is "on," it is a complete star.

Why do you want to traverse?

"Why do we have to refine a series of elements, is jquery choosing syntax not strong enough?"

OK, let's start with the example. In the page mentioned above, when a star is clicked, we need to add Class "on" to it as well as to each star on the left. At the same time, we want to change the background color of all Star parent elements, so our code is as follows:

$ (". Star"). Click (function () {

$ (this). AddClass ("on");

How do I select the parent element of the current object?

How do I get all of the star on the left of the current star?

});

In the second line, we got the current object we clicked on. But how do you get the parent of the stars? that is, div.rating. However, in a page, there are many div.rating. Which one is what we want? How do I get all of the star on the left of "this"?

The good news is that jquery allows us to get a new set of elements on the basis of these grassroots relationships based on existing collections. And that's where the traversal function works.

1, children

This function gets a direct child of a set of elements.

In many cases it would be convenient to look at the following picture:

At the beginning, all the star in the container was selected;

Pass a Select expression to children () to narrow the selection to the selected star;

If Chilidren () accepts any arguments, all direct children are returned;

Does not return a grandchild element.



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.