1. Add the Index property to the LI tag, which is exactly the index value of the span tag array
2, function encapsulation suitable for the page has more than one tab switch, you need to be aware of the LI tags and span label objects, you must pass the corresponding Div object as a parameter
Li Tag Add index property
<! DOCTYPE html>{padding:0; Margin:0; }. box1 {width:500px; height:400px; border:1px solid #b6ff00; Overflow:hidden; margin:100px Auto; } ul {width:510px; height:40px; List-Style:none; } li {float: Left; width:101px; height:40px; Text-Align:center; Font:15px/40px "SimSun"; background-Color:beige; Cursor:pointer; } span {display:none; width:500px; height:360px; Background-color: #ffd800; Text-Align:center; Font:150px/360px "SimSun";}. Current {background-color: #ffd800; }. Show {display:block; } </style>varLis = document.getElementsByTagName ("li"); varSPS = document.getElementsByTagName ("span"); for(vari = 0; i < lis.length; i++) { Lis[i].index = i; Lis[i].onmouseover=function () { for(varj = 0; J < Lis.length; J + +) {Lis[j].classname= ""; Sps[j].classname= ""; } This. ClassName = "current"; sps[ This. Index].classname = "Show"; } } </script></body>
Function encapsulated version
<! DOCTYPE html>{padding:0; Margin:0; }. box {width:500px; height:400px; border:1px solid #b6ff00; Overflow:hidden; margin:100px Auto; } ul {width:510px; height:40px; List-Style:none; } li {float: Left; width:101px; height:40px; Text-Align:center; Font:15px/40px "SimSun"; background-Color:beige; Cursor:pointer; } span {display:none; width:500px; height:360px; Background-color: #ffd800; Text-Align:center; Font:150px/360px "SimSun";}. Current {background-color: #ffd800; }. Show {display:block; } </style>varBoxs = document.getelementsbyclassname ("box"); for(vari = 0; i < boxs.length; i++) { tabswitch (boxs[i]); } functionTabswitch (ele) {varLis = Ele.getelementsbytagname ("li"); varSPS = Ele.getelementsbytagname ("span"); for(vari = 0; i < lis.length; i++) {Lis[i].index=i; Lis[i].onmouseover=function () { for(varj = 0; J < Lis.length; J + +) {Lis[j].classname= ""; Sps[j].classname= ""; } This. ClassName = "current"; sps[ This. Index].classname = "Show"; } } } </script></body>
Js--tab function Encapsulation