Locate the first li in multiple ul instances and obtain the location from multiple ulli instances.
If we only get the first li in ul, we can write as follows:
$ ("Ul li: first"); $ ("ul li "). eq (0); $ ("ul li "). first (); $ ("ul li "). slice (); // The first parameter of the slice indicates the start position of the selection, and the second parameter indicates the end position.
<Ul> <li> anzhe </li> </ul> <ul> <li> anzhe </li> </ul> <ul> <li> anzhe </li> </ul> <ul> <li> An Zhe </li> </ul>
I have written about four solutions.
<Script type = "text/javascript"> // solution 1 $ (function () {var list = $ ("li"); for (var I = 0; I <list. length; I ++) {$ ("ul: eq (" + I + ") li: first" ).css ("background", "red ");}}); // solution 2/* $ (function () {$ ("ul "). each (function () {functions (this).children().first().css ("background", "red") ;}); * // solution 3/* $ (function () {$ ("ul li: nth-child (1)" background .css ("background", "red ");}); * // solution 4/* $ (function () {$ ("ul li: first-child" ).css ("background", "red ");}); */</script>
Running result