jquery set the method of child element class

Source: Internet
Author: User

Under the class= "Www111cnnet" There are 3 UL, now want to give each UL under the 5th Li element add class= "com"

Get the number of child elements under ID=DIV1

$ (' #div1 '). Children (). length;

Gets the number of span elements under ID=DIV1

$ (' #div1 '). Children (' span '). length;

Specific jquery code:

$ (". Www111cnnet ul Li:nth-child (5)"). AddClass ("com");

has successfully given each UL under the 5th Li Add class for COM

Of course, you can also add CSS code directly:

$ (". Www111cnnet ul Li:nth-child (5)"). CSS ({"Padding-right": "5px"});

Nth-child: Abbreviation in English, n-th, for the first, 1,2,3 respectively with first, second, third words, starting from 4 to the number of words +th, such as 4th, 5th, 6th and so on

Get the first Li under the UL

$ ("ul Li:first-child")

Get the last Li under UL

$ ("ul Li:last-child")

Example

The code is as follows

<div class= "BG01" >
<ul>
<li class= "Bold" > Black domain www.111cn.net name alarm </li>
<li> Total: <span class= "MyColor" >24 strip </span></li>
<li> not processed: <span class= "myred" >03 strip </span></li>
</ul>
</div>

To get the elements above class= "Bold", write the following

The code is as follows

: $ (this). Children ("ul"). Children ("Li:first-child"). CSS ("Color", "#FF6666");

Add

To get the sibling element:

1. Next ([expr]):

Gets the next sibling element of the specified element (note that the next sibling element OH)

The code is as follows

<! DOCTYPE html>
<script type= "Text/javascript" src= "/jquery/jquery.js" ></script>

<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>
$ (' Li.third-item '). Next (). CSS (' background-color ', ' red ');
</script>

</body>

The result of this example is that only the list item 4 background color turns red

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.