<script type="text/javascript">
window.onload = function(){
var oTab = document.getElementById("tabs");
var oUl = oTab.getElementsByTagName("ul")[0];
var oLis = oUl.getElementsByTagName("li");
var oDivs= oTab.getElementsByTagName("div");
for(var i= 0,len = oLis.length;i<len;i++){
oLis[i].index = i;
oLis[i].onclick = function() {
for(var n= 0;n<len;n++){
oLis[n].className = "";
oDivs[n].className = "hide";
}
this.className = "on";
oDivs[this.index].className = "";
}
};
}
</script>
希望有所帮助
for(var i= 0,len = oLis.length;i<len;i++){ //for遍历oLis oLis[i].index = i; //保存每一个i值到对应的每一个oLis上 oLis[i].onclick = function() { //点击 for(var n= 0;n<len;n++){ //再次对oLis遍历 oLis[n].className = ""; //所有oLis的class为空 oDivs[n].className = "hide"; //所有oDiv的class为hide } this.className = "on"; //当前点击的oLis 的class为on oDivs[this.index].className = ""; //当前点击的oLis的i值所对应的oDivs的class为空 } }; }