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