标签切换效果

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }

    #contain {
      width: 100%;
      list-style: none;
      height: 60px;
      background: #c20c0c;
    }
    ul{
      width: 50%;
      list-style: none;
      height: 60px;
      margin: 0px auto;
    }
    ul li{
      float: left;
      color: white;
      font-size: 12px;
      width: 50px;
      height: 20px;
      text-align: center;
      border-radius: 10px 10px 10px 10px;
      background-color: black;
      margin: 20px;
    }

  </style>
</head>
<body>
<div id="contain">
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
  </ul>
</div>

</body>
</html>
<script type="text/javascript">
  var elementsByTagName = document.getElementsByTagName("li");
  for(var i=0;i<elementsByTagName.length;i++){
    +function (index){
      elementsByTagName[index].onmouseover=function (){
        // 遍历每一个节点为初始颜色
        for (let j = 0; j < elementsByTagName.length; j++) {
          elementsByTagName[j].style.backgroundColor="#C20C0C";
        }
        elementsByTagName[index].style.backgroundColor="rgb(0,0,0,0.3)";
        elementsByTagName[index].style.borderRadius="10px 10px 10px 10px";
      }
      elementsByTagName[index].onmouseleave=function (){
        // 遍历每一个节点为初始颜色
        for (let j = 0; j < elementsByTagName.length; j++) {
          elementsByTagName[j].style.backgroundColor="#C20C0C";
        }
        // elementsByTagName[index].style.backgroundColor="rgb(0,0,0,0.3)";
        // elementsByTagName[index].style.borderRadius="10px 10px 10px 10px";
      }

    }(i)
  }
</script>
posted @ 2024-05-13 00:07  King-DA  阅读(6)  评论(0)    收藏  举报