案例3-选项卡-另一种记录当前选择的

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

<style>

.active {

background: red;

}

div {

font: 200px "宋体";

display: none;

}

.show {

display: block;

}

</style>

</head>

<body>

<button class="btn active">按钮1</button>

<button class="btn">按钮2</button>

<button class="btn">按钮3</button>

<button class="btn">按钮4</button>

<div class="show">内容1</div>

<div>内容2</div>

<div>内容3</div>

<div>内容4</div>

<script>

var btns = document.querySelectorAll(".btn");

var inners = document.querySelectorAll("div");

var now = 0; // 记录选中的是哪一个

/*

点中当前让之前选中的,取消选中,然后给当前选中

*/

for(var i = 0; i < btns.length; i++){

// 如果需要知道当前操作的是这组中的第几个一定记得加索引

btns[i].index = i;

btns[i].onmouseover = function(){

// 清除现在选中的

btns[now].classList.remove("active");

inners[now].classList.remove("show");

// 给当前加上选中效果

this.classList.add("active");

//console.log(this.index);

inners[this.index].classList.add("show");

 

//同步当前选中的是第几个

now = this.index;

};

}

</script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值