Jqeach and jqeach

Source: Internet
Author: User

Jqeach and jqeach

1. You can use each to traverse li to obtain all li content.

<! -- 1 --> <ul class = "one"> <li> 11a </li> <li> 22b </li> <li> 33c </li> <li> 44d </li> <li> 55e </li> </ul> <button> output each li value </button> <script> // one way to traverse li through each get all li content $ ("button "). click (function () {$ (". one> li "). each (function () {// print all contents of li console. log ($ (this ). text () ;})}); </script>

2. Traverse li through each and add events to each li through $ (this ).

<! -- 2 types --> <ul class = "two"> <li> 2222 </li> <li> 22b </li> <li> 3333 </li> <li> 44d </li> <li> 5555 </li> </ul> <script> // use each to traverse li through $ (this) add an event $ ('. two> li '). each (function (index) {console. log (index + ":" + $ (this ). text (); // Add a click point to each li to change the color $ (this ). click (function () {alert ($ (this ). text (); then (this).css ("background", "# fe4365") ;}) ;}</script>

4. traverse all li resources and add class names to all li resources.

<! -- 4 types --> <ul class = "ctn3"> <li> Eat </li> <li> Sleep </li> </ ul> <span> click 3 </span> <script> // you can use four methods to traverse all li resources and add class names to all li resources $ ('span '). click (function () {$ ('. ctn3> li '). each (function () {$ (this ). toggleClass ('example ') ;}}); </script>

Five types of element == (this) in the each () loop)

<! DOCTYPE html> 

 

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.