eq(): 表示选中元素大队列的排名 , 选中元素不同, 序号也是不同
index(): 亲兄弟的排名 . 无视亲兄弟的类型 , 值非常稳定`
each() : 表示”每一个” , 会遍历 . 循环区配到的每一个元素 , 接受一个参数就是匿名函数
函数中有一个参数i , 辨识遍历到该对象的序号

1.1 eq()

  1. <div class="box">
  2. <p></p>
  3. <p></p>
  4. <p></p>
  5. <p></p>
  6. </div>
  7. <script src="js/jquery-1.9.1.js"></script>
  8. <script type="text/javascript">
  9. //点击div 让它的第一个儿子变色
  10. $("div").click(function(){
  11. $(this).children().eq(0).css("backgroundColor","red")
  12. });
  13. </script>

eq() : 选中元素大队列的排名 , 与亲兄弟的排名没有直接关系
Eq() : 选中元素不同,序号也是不相同的

  1. <div class="box">
  2. <p></p>
  3. <p></p>
  4. <p></p>
  5. <p></p>
  6. </div>
  7. <div class="box">
  8. <p></p>
  9. <p class="teshu">teshu</p>
  10. <p class="teshu">teshu</p>
  11. <p></p>
  12. <p></p>
  13. <p></p>
  14. </div>
  15. <script>
  16. //所有在div内所有p的元素进行一个排序
  17. $("div").click(function(){
  18. $("p").eq(4).css("backgroundColor","red");
  19. })
  20. //所有在div内所有p的元素进行一个排序
  21. $("div").click(function(){
  22. $(".teshu").eq(1).css("backgroundColor","red");
  23. })
  24. //Eq() : 选中元素不同,序号也是不相同的
  25. $(".teshu").eq(0).css("backgroundColor","red");
  26. </script>

1.2 index()亲兄弟的排名

Index() : 表示亲兄弟的排名 , 无视清闲哦那工地的类型, 只要是同一个父亲节点就是亲兄弟.

Dom结构:

  1. <div class="box">
  2. <ul>
  3. <li></li>
  4. </ul>
  5. <h3></h3>
  6. <p></p>
  7. <p></p>
  8. <p id="teshu" class="teshu">输出亲兄弟的排名</p>
  9. <p></p>
  10. </div>

执行代码:

  1. //点击teshu输出亲兄弟的排名序号
  2. $("#teshu").click(function(){
  3. console.log($(this).index());
  4. })

输出结果:

image.png

Index() 的值非常稳定 , 无视选择器的各种选择

Dom结果:

  1. <div class="box">
  2. <p></p>
  3. <p id="teshu" class="teshu"></p>
  4. <p id="teshu" class="teshu"></p>
  5. <p></p>
  6. <p></p>
  7. </div>
  8. <div class="box1" id="box1">
  9. <p></p>
  10. <p id="teshu" class="teshu"></p>
  11. <p id="teshu" class="teshu">弹出亲兄弟的排名</p>
  12. <p></p>
  13. <p></p>
  14. </div>

执行代码:

  1. //弹出亲兄弟的排名
  2. $(".box1 p").click(function () {
  3. console.log($(this).index());
  4. })
  5. $(".teshu").click(function(){
  6. console.log($(this).index());
  7. })

输出结果:

image.png

对应思想: 编程中比较常用的一种就是对应思想

Dom结构:

  1. <div class="box" id="hezi1">
  2. <p></p>
  3. <p ></p>
  4. <p></p>
  5. <p></p>
  6. </div>
  7. <div class="box " id="hezi2" >
  8. <p></p>
  9. <p ></p>
  10. <p></p>
  11. <p></p>
  12. </div>

执行代码:

  1. //点击box中的p 对应的box1中的p改变颜色
  2. $("#hezi1 p").click(function () {
  3. console.log($(this).index());
  4. $("#hezi2 p").eq($(this).index()).css("backgroundColor","red")
  5. })

结果:

image.png

实列选项卡

  1. <head>
  2. <style>
  3. * {
  4. padding: 0;
  5. margin: 0;
  6. }
  7. .box {
  8. width: 300px;
  9. height: 300px;
  10. border: 1px solid #000;
  11. margin: 50px auto;
  12. }
  13. .box .hd {
  14. height: 40px;
  15. }
  16. .box .hd span {
  17. float: left;
  18. width: 99px;
  19. height: 39px;
  20. border-right: 1px solid #000;
  21. border-bottom: 1px solid #000;
  22. line-height: 39px;
  23. text-align: center;
  24. background-color: #eee;
  25. }
  26. .box .hd span:last-child {
  27. border-right: none;
  28. }
  29. .box .hd span.cur {
  30. background-color: #fff;
  31. border-bottom: none;
  32. font-weight: bold;
  33. }
  34. .box .bd {
  35. height: 260px;
  36. }
  37. .box .hd div {
  38. display: none;
  39. }
  40. .box .hd div.active {
  41. display: block;
  42. }
  43. </style>
  44. </head>
  45. <body id="body">
  46. <div class="box">
  47. <div class="hd" id="hd">
  48. <span class="cur">新闻</span>
  49. <span>体育</span>
  50. <span>时尚</span>
  51. </div>
  52. <div class="bd" id="bd">
  53. <div class="active">新闻</div>
  54. <div>体育</div>
  55. <div>时尚</div>
  56. </div>
  57. </div>
  58. <div class="box">
  59. <div class="hd" id="hd">
  60. <span class="cur">新闻</span>
  61. <span>体育</span>
  62. <span>时尚</span>
  63. </div>
  64. <div class="bd" id="bd">
  65. <div class="active">新闻</div>
  66. <div>体育</div>
  67. <div>时尚</div>
  68. </div>
  69. </div>
  70. <script src="js/jquery-1.9.1.js"></script>
  71. <script type="text/javascript">
  72. //当鼠标移入hd 里的span 当前span加cur, 让bd中div 加active
  73. //绑定鼠标移入事件
  74. //写法1
  75. /* $("#hd span").mouseenter(function () {
  76. //当前span 加cur
  77. $(this).addClass("cur");
  78. //排他
  79. $(this).siblings().removeClass("cur");
  80. //bd中对应div 加active
  81. $("#bd div").eq($(this).index()).addClass("active").siblings().removeClass("active")
  82. //一般我们习惯使用节点操作
  83. $(this).parent(".bd").siblings(".bd").children("div").eq($(this).index()).addClass("active").siblings().removeClass("active");
  84. }) */
  85. //简化代码写法2 节点操作 父节点 兄弟节点
  86. $("#hd span").mouseenter(function () {
  87. $(this).addClass("cur").siblings().removeClass("cur").parent().siblings().children().eq($(this).index()).addClass("active").siblings().removeClass("active");
  88. })
  89. </script>
  90. </body>

输出结果: 有交互效果
image.png

1.3 each()用于循环遍历所区配的单词

each() : 表示 “每一个” . 用于循环. 遍历所区配到的每一个元素 , 接受一个参数就是: 匿名函数 , 函数当中也有一个 this , 表示遍历到的该对象.

Dom结构:

  1. <div class="box">
  2. <p></p>
  3. <p></p>
  4. <p></p>
  5. <p></p>
  6. </div>
  7. <div class="box">
  8. <p></p>
  9. <p></p>
  10. <p></p>
  11. <p></p>
  12. </div>
  13. <div class="box">
  14. <p></p>
  15. <p></p>
  16. <p></p>
  17. <p></p>
  18. </div>
  19. <div class="box">
  20. <p></p>
  21. <p></p>
  22. <p></p>
  23. <p></p>
  24. </div>

执行代码

  1. $('div').each(function(){
  2. // console.log(this);
  3. // console.log($(this));
  4. // $(this).children().eq(0).css('backgruondColor',"red")
  5. $(this).children().eq(1).css('backgroundColor',"red");
  6. })

结果:
image.png image.png

小案列: 隔列变色

Dom结构:
(table>tr.teshu>td4)4
执行代码:

  1. //要求 隔列变色
  2. $('tr').each(function(){
  3. console.log(this);
  4. $(this).children("td:odd").css("backgroundColor","red")
  5. $(this).children("td:even").css("backgroundColor","blue")
  6. })

结果:
image.png

each() 的参数是一个函数, 函数中有一个参数i 标识遍历到该对象的序号

小案列2 隔行变色

  1. //遍历特殊元素
  2. $('.teshu').each(function(i){
  3. console.log(i)
  4. })

image.png image.png