<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style>
      ul {
        width: 400px;
        height: 300px;
        background-color: aqua;
        list-style: none;
        padding: 10px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
      }
      li {
        width: 100%;
        height: 40px;
        background-color: rgb(180, 236, 124);
        color: white;
        font-size: 20px;
      }
      .active {
        background-color: red;
      }
    </style>
    <title>模拟自动点击事件</title>
  </head>
  <body>
    <ul>
      <li class="item active">1</li>
      <li class="item">2</li>
      <li class="item">3</li>
      <li class="item">4</li>
      <li class="item">5</li>
    </ul>
  </body>
</html>
<script>
  // 获取li
  var li_ = document.getElementsByTagName("li");
  // 循环遍历li
  for (let i = 0; i < li_.length; i++) {
    // 给每一个li添加一个自定义class名
    li_[i].setAttribute("index", i);
    // 当点击每一个li时
    li_[i].onclick = function () {
      // 获取到自定义下标
      var index = this.getAttribute("index");
      // 再次循环遍历
      for (let j = 0; j < li_.length; j++) {
        // li_[j].className = "";
        // li_[index].className = "active";
        // 进来先删除样式
        li_[j].classList.remove("active");
        // 在给循环每一个添加样式
        li_[index].classList.add("active");
      }
    };
  }

  // 模拟自动点击事件

  var ind = 0;
  // 放入计时器判断
  setInterval(function () {
    ind++;
    // 判断如果ind>li_.length说明已经走了一遍
    if (ind >= li_.length) {
      // 再次回到起点
      ind = 0;
    }
    // 调用自动点击事件
    li_[ind].click();
  }, 1000);
</script>