• 11a
  • 22b
  • 33c
  • 44d
  • 55e
  • "/>
  • 11a
  • 22b
  • 33c
  • 44d
  • 55e
  • ">

    Home  >  Article  >  Web Front-end  >  Detailed explanation of example code for each traversal event, class, and loop in jquery

    Detailed explanation of example code for each traversal event, class, and loop in jquery

    伊谢尔伦
    伊谢尔伦Original
    2017-07-19 14:36:231941browse

    Traverse li through each to get all the contents of li


    <!-- 1种 -->
      <ul class="one">
        <li>11a</li>
        <li>22b</li>
        <li>33c</li>
        <li>44d</li>
        <li>55e</li>
      </ul>
      <button>输出每个li值</button>
    <script>
      // 1种 通过each遍历li 可以获得所有li的内容
      $("button").click(function(){ 
        $(".one > li").each(function(){
          // 打印出所有li的内容
          console.log($(this).text());
        })
      });
    </script>

    Traverse li through each and pass $(this) to each Add event to li


    <!-- 2种 -->
      <ul class="two">
        <li>2222</li>
        <li>22b</li>
        <li>3333</li>
        <li>44d</li>
        <li>5555</li>
      </ul>
    <script>
      // 2种 通过each遍历li 通过$(this)给每个li加事件
      $(&#39;.two > li&#39;).each(function(index) {
        console.log(index +":" + $(this).text());
        // 给每个li加click 点那个就变颜色
        $(this).click(function(){
          alert($(this).text());
          $(this).css("background","#fe4365");
        });
      });
    </script>

    Traverse all li and add class name to all li


    <!-- 4种 -->
      <ul class="ctn3">
        <li>Eat</li>
        <li>Sleep</li>
        <li>3种</li>
      </ul>
      <span>点击3</span>
    <script>
      // 4种 遍历所有li 给所有li添加 class类名
      $(&#39;span&#39;).click(function(){
        $(&#39;.ctn3 > li&#39;).each(function(){
          $(this).toggleClass(&#39;example&#39;);
        })
      });
    </script>

    In each() loop element == $(this)


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>each练习2</title>
      <style>
        p {
          width: 40px;
          height: 40px;
          margin: 5px;
          float: left;
          border: 2px blue solid;
          text-align: center;
        }
        span {
          width: 40px;
          height: 40px;
          color: red;
        }
      </style>
    </head>
    <body>
      <p></p>
      <p></p>
      <p></p>
      <p id="stop">Stop here</p>
      <p></p>
      <p></p>
      <button>Change colors</button>
      <span></span>
    </body>
    <script src="jquery-1.11.1.min.js"></script>
    <script >
       // 在each()循环里 element == $(this)
      $(&#39;button&#39;).click(function(){
        $(&#39;p&#39;).each(function(index,element){
          //element == this;
          $(element).css("background","yellow");
    
          if( $(this).is("#stop")){
            $(&#39;span&#39;).text("index :" + index);
            return false;
          }
        })
      })
    </script>
    </html>

    The above is the detailed content of Detailed explanation of example code for each traversal event, class, and loop in jquery. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn