这是我的 html 与 css 和 javascript 都在一个地方
JS
var links = document.getElementsByTagName("a"); //get the links
var len = links.length;
for(var i = 0; i<len; i++) {
links[i].onclick = handleClick; // add onclick handler
}
function handleClick(e){
var target = e.target;
var id = target.id + "content";
document.getElementById(id).style.zIndex = 10;
}
HTML
<div id="tabbed">
<a href="#" id="tabe1">Tabe1</a>
<div class="section" id="tabe1content">
<div>
<p> content1 </p>
</div>
</div>
<a href="#" id="tabe2">Tabe2</a>
<div class="section" id="tabe2content">
<div>
<p> content2 </p>
</div>
</div>
<a href="#" id="tabe3">Tabe3</a>
<div class="section" id="tabe3content">
<div>
<p> content3 </p>
</div>
</div>
</div>
CSS
.section{
position:absolute;
float:left;
width:500px;
background-color:gray;
left:0;
top:0;
height:300px;
margin-top:30px;
}
#tabbed{
position:relative;
}
a {
margin-right:10px;
float:left;
display:block;
}
当我测试它时,它只工作一次。第二次点击表1时,它仍然显示表3。请看看有什么问题,还有其他比我更好的方法吗?