一、在網頁製做過程當中,咱們常常會用到選項卡切換效果,它可以讓咱們的網頁在交互和佈局上都能獲得提高css
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>選項卡</title> <style type="text/css"> #tab{width:300px;border:3px solid #CCC;} #tab .active{background-color:#FFF} #tab h3{ margin:0px; padding:0px; font-size:14px; float:left; background-color:#CCC; width:60px; height:24px; line-height:24px; text-align:center; } #tab div{ clear:both; height:100px; font-size:14px; padding:20px 0px 0px 20px; display:none; } </style> <script> window.onload=function(){ var oTab=document.getElementById("tab"); var aH3=oTab.getElementsByTagName("h3"); var aDiv=oTab.getElementsByTagName("div"); for(var i=0;i<aH3.length;i++){ aH3[i].index=i; aH3[i].onclick=function(){ for(var i=0;i<aH3.length;i++){ aH3[i].className=""; aDiv[i].style.display="none"; } this.className="active"; aDiv[this.index].style.display="block"; } } } </script> </head> <body> <div id="tab"> <h3 class="active">教育</h3> <h3>娛樂</h3> <h3>汽車</h3> <div style="display:block">教育的內容</div> <div>娛樂的內容</div> <div>汽車的內容</div> </body> </html>