- 公告
- 规则
- 论坛
- 公益
- 安全
// 获得属性
function TabFn() {
this.tabLi = $('tab_top').getElementsByTagName('li');
this.tabC = $('tab_bottom').getElementsByClassName('tab-content');
}
// 定义原型方法
TabFn.prototype = {
// 1.初始化事件
initEvent: function () {
this.setIndex();
this.bindEvent();
},
// 2.设置索引
setIndex: function () {
for (var i = 0; i < this.tabLi.length; i++) {
var li = this.tabLi[i];
li.index = i;
}
},
// 3.绑定事件
bindEvent: function () {
for (var i = 0; i < this.tabLi.length; i++) {
var own = this;
this.tabLi[i].onmouseover = function () {
own.handler(this);
}
}
},
// 4.事件处理函数
handler: function (that) {
for (var j = 0; j < this.tabLi.length; j++) {
this.tabLi[j].className = '';// !驼峰结构
this.tabC[j].style.display = 'none';
}
// that = li.current;
that.className = 'current';// that 为当前的tab上的li
this.tabC[that.index].style.display = 'block';
}
}
window.onload = function () {
var tab = new TabFn();
tab.initEvent();
}
> 请问下这里setIndex的作用
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
setIndex的作用是给top列表中的元素设置索引,设置索引的目的是因为bindEvent的时候不能传递索引i,因为循环执行完毕后,i始终等于this.tabLi.length,而在设置选项卡内容是否隐藏时
你需要知道,当前是操作的是第几个li,这也就是setIndex的目的。