DIV设置
* { padding: 0; margin: 0; }
li { list-style: none; }
body { background: #f6f9fc; font-family: arial; }
.calendar { width: 210px; margin: 0 auto; padding: 10px 10px 20px 20px; background: #eae9e9; }
.calendar ul { width: 210px; overflow: hidden; padding-bottom: 10px; }
.calendar li { float: left; width: 58px; height: 54px; margin: 10px 10px 0 0; border: 1px solid #fff; background: #424242; color: #fff; text-align: center; cursor: pointer; }
.calendar li h2 { font-size: 20px; padding-top: 5px; }
.calendar li p { font-size: 14px; }
.calendar .active { border: 1px solid #424242; background: #fff; color: #e84a7e; }
.calendar .active p { font-weight: bold; }
.calendar .text { width: 178px; padding: 0 10px 10px; border: 1px solid #fff; padding-top: 10px; background: #f1f1f1; color: #555; }
.calendar .text h2 {font-size: 14px; margin-bottom: 10px; }
.calendar .text p { font-size: 12px; line-height: 18px; }
页面设置
<div id="tab" class="calendar">
<ul id="box">
<li class="active"><h2>1</h2><p>JAN</p></li>
<li ><h2>2</h2><p>FER</p></li>
<li ><h2>3</h2><p>MAR</p></li>
<li ><h2>4</h2><p>APR</p></li>
<li ><h2>5</h2><p>MAY</p></li>
<li><h2>6</h2><p>JUN</p></li>
<li><h2>7</h2><p>JUL</p></li>
<li><h2>8</h2><p>AUG</p></li>
<li><h2>9</h2><p>SEP</p></li>
<li><h2>10</h2><p>OCT</p></li>
<li><h2>11</h2><p>NOV</p></li>
<li><h2>12</h2><p>DEC</p></li>
</ul>
<div class="text" id="txt">
<h2>1月活动</h2>
<p>快过年了,大家可以商量着去哪玩吧~</p>
</div>
</div>
js
var arr=[‘快过年了,大家可以商量着去哪玩吧~’,
‘大家好好学习吧222222~~~’,
‘大家好好学习吧222222333~~~’,
‘大家好好学习吧222444222~~~’,
‘大家好好学习555吧222222~~~’,
‘大家好好学习吧666222222~~~’,
‘大家好好学习吧227772222~~~’,
‘大家好好学习吧28888822222~~~’,
‘大家好好学习吧99999222222~~~’,
‘大家好好学习10000000吧222222~~~’,
‘大家好好学习吧111111222222~~~’,
‘大家好好学习吧22222200000000000~~~’];
var otab = document.getElementById("tab");
var ali = otab.querySelectorAll("li");
var oh2 = document.querySelector(".text h2");
var op = document.querySelector(".text p");
// 1.遍历所有li,准备绑定事件
for(var i=0;i<ali.length;i++){
// 5.在还能够拿到每一个li和每一个i的时候,给li自定义索引,以供将来使用
// ali[i].setAttribute("liyang",i)
ali[i].liyang = i;
// 2.开始绑定事件
ali[i].onclick = function(){
// 3.先清除所有的class
for(var j=0;j<ali.length;j++){
ali[j].className = "";
}
// 注意:此时在事件中,已经拿不到外面的循环的计数器了
// 因为循环已经执行结束了
// console.log(i)
// console.log(this)
// 4.找到当前元素,添加class
this.className = "active";
// 6.根据当元素,找到当初自定义的索引,根据索引,找到对应的数据
// console.log(this.getAttribute("liyang"))
// console.log(this.liyang)
oh2.innerHTML = this.liyang+1 + "月活动";
op.innerHTML = arr[this.liyang];
}
}