Locate the first li in multiple ul instances and obtain the location from multiple ulli instances.

Source: Internet
Author: User

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

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.