How jquery sets sub-element class

Source: Internet
Author: User

How jquery sets sub-element class

There are 3 ul under class = "www111cnnet". Now we want to add class = "com" to the 5th li elements under each ul"

// Obtain the number of child elements under id = div1

$ ('# Div1'). children (). length;

// Obtain the number of span elements under id = div1

$ ('# Div1'). children ('span '). length;

Specific jQuery code:

$ (". Www111cnnet ul li: nth-child (5)"). addClass ("com ");

You have successfully added the class com to the 5th li in each ul.

Of course, you can also directly add the CSS code:

$ (". Www111cnnet ul li: nth-child (5)" ).css ({"padding-right": "5px "});

Nth-child: the abbreviation in English, n-th, which indicates the nth number, 1, 2, and 3 respectively. They use the first, second, and third words respectively, and start from 4 as the number word + th, such as 4th, 5th, and 6th

Obtain the first li in ul.

$ ("Ul li: first-child ")

Obtain the last li in ul.

$ ("Ul li: last-child ")

Example

The Code is as follows:  

<Div class = "Bg01">
<Ul>
<Li class = "Bold"> black domain www.111cn.net name warning </li>
<Li> total: <span class = "Mycolor"> 24 </span> </li>
<Li> not handled: <span class = "Myred"> 03 </span> </li>
</Ul>
</Div>

To obtain the above class = "Bold" element, write as follows:

The Code is as follows:  

: $ (This). children ("ul"). children ("li: first-child" ).css ("color", "# FF6666 ");

Supplement

Obtain the same level element:

1. next ([expr]):

Obtain the next sibling element of a specified Element (note that it is the next sibling element)

The Code is as follows:  

<! DOCTYPE html>
<Html>
<Head>
<Script type = "text/javascript" src = "/jquery. js"> </script>
</Head>

<Body>
<Ul>
<Li> list item 1 </li>
<Li> list item 2 </li>
<Li class = "third-item"> list item 3 </li>
<Li> list item 4 </li>
<Li> list item 5 </li>
</Ul>

<Script>
Certificate ('li.third-item'0000.next().css ('background-color', 'red ');
</Script>

</Body>
</Html>

In this example, only the list item 4 background color turns red.

Related Article

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.