JS面向?qū)ο缶幊虒?shí)現(xiàn)的Tab選項(xiàng)卡案例詳解
本文實(shí)例講述了JS面向?qū)ο缶幊虒?shí)現(xiàn)的Tab選項(xiàng)卡。分享給大家供大家參考,具體如下:
Tab選項(xiàng)卡案例

下面是一個(gè)簡(jiǎn)單面向過(guò)程的Tab選項(xiàng)卡。
<!DOCTYPE html>
<html>
<head>
<style>
#tabBox input {
background: #F6F3F3;
border: 1px solid #FF0000;
}
#tabBox .active {
background: #E9D4D4;
}
#tabBox div {
width:300px;
height:250px;
display:none;
padding: 10px;
background: #E9D4D4;
border: 1px solid #FF0000;
}
</style>
<meta charset="utf-8" />
<title>選項(xiàng)卡</title>
<script>
window.onload=function(){
var tabBox = document.getElementById('tabBox');
var tabBtn = tabBox.getElementsByTagName('input');
var tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<tabBtn.length;i++){
tabBtn[i].index = i;
tabBtn[i].onclick = function (){
for(var j=0;j<tabBtn.length;j++){
tabBtn[j].className='';
tabDiv[j].style.display='none';
}
this.className='active';
tabDiv[this.index].style.display='block';
};
}
};
</script>
</head>
<body>
<div id="tabBox">
<input type="button" value="主頁(yè)" class="active" />
<input type="button" value="說(shuō)說(shuō)" />
<input type="button" value="日志" />
<div style="display:block;">這是主頁(yè)內(nèi)容</div>
<div>這是說(shuō)說(shuō)內(nèi)容</div>
<div>這是日志內(nèi)容</div>
</div>
</body>
</html>
下面來(lái)慢慢改成面向?qū)ο蟮男问健?/p>
1.首先將嵌套的函數(shù)拿到window.onload外面,不能有函數(shù)嵌套,可以有全局變量。如下:所有的改寫(xiě)最終效果都不變。
<script>
//將在嵌套函數(shù)里的變量提取到全局中
var tabBtn = null;
var tabDiv = null;
window.onload = function(){
var tabBox = document.getElementById('tabBox');
tabBtn = tabBox.getElementsByTagName('input');
tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<tabBtn.length;i++){
tabBtn[i].index = i;
//此處調(diào)用函數(shù)即可
tabBtn[i].onclick = clickBtn;
}
};
//將嵌套函數(shù)提取到全局中
function clickBtn(){
for(var j=0;j<tabBtn.length;j++){
tabBtn[j].className='';
tabDiv[j].style.display='none';
}
this.className='active';
tabDiv[this.index].style.display='block';
};
</script>
2.將全局的變量變?yōu)閷?duì)象的屬性,全局的函數(shù)變?yōu)閷?duì)象的方法;將window.onload里的代碼提取到一個(gè)構(gòu)造函數(shù)里面,在window.onload里創(chuàng)建對(duì)象即可;(下面的代碼執(zhí)行起來(lái)是有問(wèn)題的)。
這里必須注意:在構(gòu)造函數(shù)Tab里的this跟之前this所代表的是不同的(此處是通過(guò)new來(lái)創(chuàng)建對(duì)象的);在上面的示例中,this指的是調(diào)用者;在構(gòu)造函數(shù)里,this指向的是var tab = new Tab() ,即tab這個(gè)對(duì)象,注意是對(duì)象。
說(shuō)一下這段代碼的問(wèn)題:我們?cè)赥ab的原型上添加clickBtn方法后,clickBtn方法里的this本應(yīng)該是指向var tab = new Tab()的,但是我們?cè)?this.tabBtn[i].onclick = this.clickBtn; 將clickBtn添加給了this.tabBtn[i],即input按鈕,clickBtn的所屬由Tab對(duì)象變成了input按鈕。
clickBtn的所屬變成input按鈕后,那么clickBtn里的this指向按鈕,那再來(lái)看clickBtn里的代碼,this.tabBtn、this.tabDiv,input按鈕里有這兩個(gè)屬性嗎?沒(méi)有,所以會(huì)出錯(cuò)!

<script>
window.onload = function(){
var tab = new Tab("tabBox");
}
/**
* 將之前window.onload里的代碼提到一個(gè)構(gòu)造函數(shù)里
* [可以將這個(gè)Tab構(gòu)造函數(shù)想象成一個(gè)Tab類(lèi)]
* @param {Object} id:選項(xiàng)卡id以參數(shù)的形式傳入
*/
function Tab(id){
var tabBox = document.getElementById(id);
//將之前的全局變量變?yōu)閷?duì)象的屬性
this.tabBtn = tabBox.getElementsByTagName('input');
this.tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<this.tabBtn.length;i++){
this.tabBtn[i].index = i;
//此處這種方式調(diào)用函數(shù),已經(jīng)將clickBtn的所屬變成this.tabBtn[i]
this.tabBtn[i].onclick = this.clickBtn;
}
};
//將之前的全局函數(shù)添加到構(gòu)造函數(shù)的原型里,作為對(duì)象的一個(gè)方法
Tab.prototype.clickBtn = function(){
alert(this); //HTMLInputElement
for(var j=0;j<this.tabBtn.length;j++){
this.tabBtn[j].className='';
this.tabDiv[j].style.display='none';
}
this.className='active';
this.tabDiv[this.index].style.display='block';
};
</script>
3.將clickBtn的調(diào)用放在一個(gè)函數(shù)里,這樣就不會(huì)改變clickBtn的所屬了。alert(this);此時(shí)彈出的是一個(gè)Object,說(shuō)明clickBtn的所屬關(guān)系沒(méi)變,還是Tab對(duì)象。但是還有另一個(gè)問(wèn)題,此時(shí)clickBtn里的this指向tab對(duì)象,那么this.className、this.index,此處的this指的是tab對(duì)象,那么對(duì)象中有這兩個(gè)屬性嗎?沒(méi)有,還會(huì)出錯(cuò)!所以第4步繼續(xù)改造。

window.onload = function(){
var tab = new Tab("tabBox");
}
/**
* 選項(xiàng)卡
* @param {Object} id:選項(xiàng)卡id
*/
function Tab(id){
var tabBox = document.getElementById(id);
this.tabBtn = tabBox.getElementsByTagName('input');
this.tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<this.tabBtn.length;i++){
this.tabBtn[i].index = i;
//將this保存成一個(gè)變量,就可以在下面代碼中調(diào)用對(duì)象的方法了
var _this = this;
//此處這種方式調(diào)用函數(shù),就不會(huì)改變clickBtn方法的所屬關(guān)系
this.tabBtn[i].onclick = function(){
//注意此處不能直接使用this,this指向this.tabBtn[i]
_this.clickBtn();
};
}
};
//點(diǎn)擊選項(xiàng)卡按鈕
Tab.prototype.clickBtn = function(){
alert(this); //Object
for(var j=0;j<this.tabBtn.length;j++){
this.tabBtn[j].className='';
this.tabDiv[j].style.display='none';
}
this.className='active';
this.tabDiv[this.index].style.display='block';
};
4. 以參數(shù)的形式將點(diǎn)擊的按鈕傳入clickBtn中
window.onload = function(){
var tab = new Tab("tabBox");
}
/**
* 選項(xiàng)卡
* @param {Object} id:選項(xiàng)卡id
*/
function Tab(id){
var tabBox = document.getElementById(id);
this.tabBtn = tabBox.getElementsByTagName('input');
this.tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<this.tabBtn.length;i++){
this.tabBtn[i].index = i;
var _this = this;
this.tabBtn[i].onclick = function(){
//注意參數(shù)this代表的是this.tabBtn[i],即input按鈕
_this.clickBtn(this);
};
}
};
//將點(diǎn)擊的按鈕以參數(shù)的形式傳入
Tab.prototype.clickBtn = function(btn){
for(var j=0;j<this.tabBtn.length;j++){
this.tabBtn[j].className='';
this.tabDiv[j].style.display='none';
}
btn.className='active';
this.tabDiv[btn.index].style.display='block';
};
5.最終版 —— 將代碼提取到一個(gè)單獨(dú)的js文件中,在用的時(shí)候引入即可。一般花大時(shí)間去寫(xiě)一個(gè)面向?qū)ο蟮某绦?,就是為了能夠?fù)用,以及方便的使用。
Tab.js
/**
* 選項(xiàng)卡
* @param {Object} id 選項(xiàng)卡id
*/
function Tab(id){
var tabBox = document.getElementById(id);
this.tabBtn = tabBox.getElementsByTagName('input');
this.tabDiv = tabBox.getElementsByTagName('div');
for(var i=0;i<this.tabBtn.length;i++){
this.tabBtn[i].index = i;
var _this = this;
this.tabBtn[i].onclick = function(){
_this.clickBtn(this);
};
}
};
/**
* 為T(mén)ab原型添加點(diǎn)擊選項(xiàng)卡方法
* @param {Object} btn 點(diǎn)擊的按鈕
*/
Tab.prototype.clickBtn = function(btn){
for(var j=0;j<this.tabBtn.length;j++){
this.tabBtn[j].className='';
this.tabDiv[j].style.display='none';
}
btn.className='active';
this.tabDiv[btn.index].style.display='block';
};
使用:tab.html 可以看到使用的時(shí)候,就可以很簡(jiǎn)單的創(chuàng)建兩個(gè)選項(xiàng)卡出來(lái)了。
<!DOCTYPE html>
<html>
<head>
<style>
.tab input {
background: #F6F3F3;
border: 1px solid #FF0000;
}
.tab .active {
background: #E9D4D4;
}
.tab div {
width:300px;
height:250px;
display:none;
padding: 10px;
background: #E9D4D4;
border: 1px solid #FF0000;
}
</style>
<meta charset="utf-8" />
<title>選項(xiàng)卡</title>
<!-- 引入tab.js -->
<script type="text/javascript" src="../js/tab.js" ></script>
<script>
window.onload = function(){
var tab1 = new Tab("tabBox1");
var tab2 = new Tab("tabBox2");
}
</script>
</head>
<body>
<div class="tab" id="tabBox1">
<input type="button" value="主頁(yè)" class="active" />
<input type="button" value="說(shuō)說(shuō)" />
<input type="button" value="日志" />
<div style="display:block;">這是主頁(yè)內(nèi)容</div>
<div>這是說(shuō)說(shuō)內(nèi)容</div>
<div>這是日志內(nèi)容</div>
</div>
<br />
<div class="tab" id="tabBox2">
<input type="button" value="技術(shù)" class="active" />
<input type="button" value="工具" />
<input type="button" value="網(wǎng)站" />
<div style="display:block;">Js、Vue</div>
<div>VSCode</div>
<div>CSDN</div>
</div>
</body>
</html>

再來(lái)簡(jiǎn)單總結(jié)一下JS面向?qū)ο笾械膖his,this一般會(huì)在兩種情況下出問(wèn)題,一是使用定時(shí)器、二是事件,從上面的例子中也可以看出來(lái)。注意下面的說(shuō)法是在構(gòu)造函數(shù)里哦,其它情況下,this指向的是調(diào)用者。
可以看到效果沒(méi)有將姓名顯示出來(lái),其實(shí)看到這里原因應(yīng)該很清楚了,就是第14行代碼中this.name,此處的this指向誰(shuí)?指向window,因?yàn)閟etInterval是屬于window的。
<!DOCTYPE html>
<html>
<meta charset="UTF-8" />
<head>
<script>
function Person(name){
this.name = name;
//定時(shí)器
setInterval(this.showName, 3000);
}
Person.prototype.showName = function(){
alert(this); //window
alert("姓名:"+this.name);
}
var p1 = new Person("jiangzhou");
</script>
</head>
</html>
解決辦法:上面例子中已經(jīng)列出來(lái)了,就是用一個(gè)function將要執(zhí)行的代碼包起來(lái),使其所屬關(guān)系不會(huì)發(fā)生變化,注意function里調(diào)用方法時(shí)使用的是外部變量'_this'。事件的處理在上面的例子中已經(jīng)說(shuō)明了。
function Person(name){
this.name = name;
var _this = this;
setInterval(function(){
this.showName();
}, 3000);
}
Person.prototype.showName = function(){
alert(this); //[Object Object]
alert("姓名:"+this.name); //姓名:jianghzou
}
var p1 = new Person("jiangzhou");
感興趣的朋友可以使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《javascript面向?qū)ο笕腴T(mén)教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript關(guān)于復(fù)選框的實(shí)用腳本代碼
javascript關(guān)于復(fù)選框的實(shí)用腳本代碼...2007-08-08
javascript getElementsByClassName實(shí)現(xiàn)代碼
根據(jù)元素clsssName得到元素集合的函數(shù),需要的朋友可以參考下。2010-10-10
詳解微信小程序開(kāi)發(fā)聊天室—實(shí)時(shí)聊天,支持圖片預(yù)覽
這篇文章主要介紹了微信小程序?qū)崟r(shí)聊天支持圖片預(yù)覽,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
JS實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)漸變背景色的方法
這篇文章主要介紹了JS實(shí)現(xiàn)進(jìn)入頁(yè)面時(shí)漸變背景色的方法,涉及javascript操作css控制背景色漸變的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
郁悶!ionic中獲取ng-model綁定的值為undefined如何解決
很是郁悶!ionic中獲取ng-model綁定的值為undefined,如何解決?2016-08-08

