选项卡--原生js

时间:2021-08-24 06:30:17

比较基础的一个特效,毕业设计要做一些记录,就把这些都记下来吧,万一用到的时候忘了呢

 1 function changeTab() {
 2     let navs = document.getElementsByTagName("li");
 3     for (let i = 0; i < navs.length; i++) {
 4         (function (i) {
 5             navs[i].onclick = function () {
 6                 for (let j = 0; j < navs.length; j++) {
 7                     // 取消所有的 active 属性
 8                     navs[j].className = "";
 9                     // do something
10                 }
11                 // 给当前点击对象添加 active 属性
12                 this.className = "active";
13                 // do something
14             }
15         })(i);
16     }
17 }

关于选中时样式的改变……添加个active就好了吧

1 .active {
2     // 各种样式
3     // eg.
4     background: red;
5     color: #fff;
6     ...   
7 }

呃……感觉就这些,拿小本本记下(^-^)