html part
Document
css part
*{ padding:0px; margin:0px; list-style: none; font-size: 14px; } #tab{ width:298px; height:120px; margin:10px; border:1px solid #eee; overflow: hidden; } .tab-nav{ width:400px; position:relative; height:27px; } .tab-nav ul{ position:absolute; width:301px; left:-1px; background-color: #f7f7f7; } .tab-nav li{ float:left; width:58px; padding: 0 1px; height:36px; background-color: #f7f7f7; border-bottom: 1px solid #eee; text-align: center; } .tab-nav li.active{ background-color: #fff; border-bottom-color:#fff; border-left: 1px solid #eee; border-right: 1px solid #eee; padding:0px; font-weight: bolder; } li a:link,li a:visited{ text-decoration: none; color:#000; } #tab-contain .mod{ margin:25px 6px 10px 6px; } #tab-contain .mod ul li{ float: left; width:143px; height:25px; overflow: hidden; }
Js part
function $(id){ return typeof id==='string'?document.getElementById(id):id; } window.onload=function(){ var navs=$('tab-nav').getElementsByTagName('li'); var ps=$('tab-contain').getElementsByTagName('p'); // alert(ps.length); if(navs.length!=ps.length){ return; } for(var i=0;i Copy after login
The above is the detailed content of Sample code for implementing Tab switching effect using JS. For more information, please follow other related articles on the PHP Chinese website!