JavaScript實現Tab標籤頁切換的最簡便方式
阿新 • • 發佈:2019-02-10
先說一下最土的一種方法:
Html:
<div class="tab-head"> <h2 id="tab1" onmouseover="changeTab1()" class="selected">1</h2> <h2 id="tab2" onmouseover="changeTab2()">2</h2> <h2 id="tab3" onmouseover="changeTab3()">3</h2> </div> <div class="tab-content"> <div id="c1" class="show">content1</div> <div id="c2">content2</div> <div id="c3">content3</div> </div>
CSS:
h2 { border-top: solid cornflowerblue 1px; border-left: solid cornflowerblue 1px; width: 50px; height: 25px; margin: 0; float: left; text-align: center; } .tab-content { border: solid cornflowerblue 1px; width: 152px; height: 100px; } .tab-content div{ display: none; } .selected { background-color: cornflowerblue; } .tab-content .show{ display: block; }
JS:
var tab1 = document.getElementById('tab1'), tab2 = document.getElementById('tab2'), tab3 = document.getElementById('tab3'), c1 = document.getElementById('c1'), c2 = document.getElementById('c2'), c3 = document.getElementById('c3'); function changeTab1() { tab1.className = 'selected'; tab2.className = ''; tab3.className = ''; c1.className = 'show' c2.className = ''; c3.className = ''; } function changeTab2() { tab1.className = ''; tab2.className = 'selected'; tab3.className = ''; c1.className = ''; c2.className = 'show'; c3.className = ''; } function changeTab3() { tab1.className = ''; tab2.className = ''; tab3.className = 'selected'; c1.className = '' c2.className = ''; c3.className = 'show'; }
效果:
實現Tab的切換,我們很容易想到的一種方式就是給每一個要控制的標籤新增id,然後分別編寫滑鼠事件,使用id獲取每個元素,精確地控制每個元素的樣式。
這種方式的缺點顯而易見,有幾個元素就有幾個id,每個tab都要編寫function,裡面的方法大同小異。要增加tab的話,還要增加id和function,程式碼冗餘,不易擴充套件。
第二種較為高明些的方法是編寫一個function,將每個元素的序號傳進去。
Html:
<div class="tab-head"> <h2 onmouseover="changeTab(0)" class="selected">1</h2> <h2 onmouseover="changeTab(1)">2</h2> <h2 onmouseover="changeTab(2)">3</h2> </div> <div class="tab-content"> <div class="show">content1</div> <div>content2</div> <div>content3</div> </div>
JS:
var tabs = document.getElementsByClassName('tab-head')[0].getElementsByTagName('h2'), contents = document.getElementsByClassName('tab-content')[0].getElementsByTagName('div'); function changeTab(index) { for(var i = 0, len = tabs.length; i < len; i++) { if(i === index) { tabs[i].className = 'selected'; contents[i].className = 'show'; }else{
tabs[i].className = ''; contents[i].className = '';
} } }
這樣就只要寫一個function了,而且不需要id,但是還是要按照順序傳遞引數。
第三種方式和第二種基本一樣,只是引數傳遞的是this指標。
Html:
<div class="tab-head"> <h2 onmouseover="changeTab(this)" class="selected">1</h2> <h2 onmouseover="changeTab(this)">2</h2> <h2 onmouseover="changeTab(this)">3</h2> </div> <div class="tab-content"> <div class="show">content1</div> <div>content2</div> <div>content3</div> </div>
JS:
var tabs = document.getElementsByClassName('tab-head')[0].getElementsByTagName('h2'), contents = document.getElementsByClassName('tab-content')[0].getElementsByTagName('div'); function changeTab(tab) { for(var i = 0, len = tabs.length; i < len; i++) { if(tabs[i] === tab) { tabs[i].className = 'selected'; contents[i].className = 'show'; } else { tabs[i].className = ''; contents[i].className = ''; } } }
這種方式稍微方便一些,只要傳遞this指標,不用按照順序傳遞序號,但這也不是最簡便的方式。
最簡便的一種:
第四種方式:
Html:
<div class="tab-head"> <h2 class="selected">1</h2> <h2>2</h2> <h2>3</h2> </div> <div class="tab-content"> <div class="show">content1</div> <div>content2</div> <div>content3</div> </div>
JS:
var tabs = document.getElementsByClassName('tab-head')[0].getElementsByTagName('h2'), contents = document.getElementsByClassName('tab-content')[0].getElementsByTagName('div'); (function changeTab(tab) { for(var i = 0, len = tabs.length; i < len; i++) { tabs[i].onmouseover = showTab; } })(); function showTab() { for(var i = 0, len = tabs.length; i < len; i++) { if(tabs[i] === this) { tabs[i].className = 'selected'; contents[i].className = 'show'; } else { tabs[i].className = ''; contents[i].className = ''; } } }
這樣JS、Html、CSS就完全分離了,通過this指標就可以判斷當前滑鼠滑過的是哪一個tab了。