zoukankan      html  css  js  c++  java
  • jquery 中 eq()遍历方法 和:eq()选择器的区别

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    *{margin: 0;padding: 0;list-style: none;}
    .box{ 100px;height: 404px;border:1px solid red;margin: 0 auto;}
    li{ 100px;height: 100px;font-size: 40px;line-height: 100px;color: #ccc;text-align: center;border-bottom: 1px solid red;}
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script>
    $(function(){
    // eq()方法的定义和用法:
    // 此方法能够获取匹配元素集上的相应位置索引的元素。
    // 匹配元素集上元素的位置索引是从0开始的。
    //
    // :eq()选择器的定义和用法:
    // 此选择器匹配一个给定索引值的元素。
    // 元素的位置索引从0算起。
    //
    // 可以直接使用.eq(index),
    // 若要使用eq选择器则要写成:eq(‘+index+’)这样获取到的index才是变量

    var index=0;
    $("li:eq(index)").css("background","red")// 错误


    $("li:eq("+index+")").css("background","blue") //正确
    $("li").eq(index+1).css("background","blue")//正确
    })
    </script>
    </head>
    <body>
    <div class="box">
    <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    </ul>
    </div>
    </body>
    </html>

  • 相关阅读:
    scala
    数据结构(01)
    基本算法(07)
    基本算法(06)
    基本算法(05)
    git pull文件时和本地文件冲突的问题
    获取两个日期之间的日期形成一个集合
    lombok的简单介绍(2)
    springboot启动报错
    逆向工程的创建
  • 原文地址:https://www.cnblogs.com/wyan20/p/5676040.html
Copyright © 2011-2022 走看看