点击某一个标签的时候实时获取该标签在这类标签中的索引值,以便进行其他操作。

在div中,有一类div标签,class值为div1,数量不等,有多个。我需要在点击某一个标签的时候实时获取该标签在这类标签中的索引值,以便进行其他操作。
<!DOCTYPE html>
<html>
<head>
  <style>
  div { width:60px; height:60px; margin:10px; float:left;
        border:2px solid blue; }
  .blue { background:blue; }
  </style>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>

<body>
  <div class="div1"></div>
  <div class="div1"></div>
  <div class="div1"></div>

  <div class="div1"></div>
  <div class="div1"></div>
  <div class="div1"></div>

  <script>
  $(".div1").click(function(){
  var index=$(".div1").index($(this));
  console.log(index);
  alert(index);
  })
  </script>
</body>
</html>

  

posted @ 2020-08-06 11:42  挥不去的执念  阅读(210)  评论(0编辑  收藏  举报