}
//變量定義
var listBox=iBase.GetByClass('js','div');
var navItem=iBase.Id('demo').getElementsByTagName('h2');//此處將jQ區(qū)塊中的h2也取到了,所以頁(yè)面會(huì)有個(gè)小小的錯(cuò)誤
var icoItem=null,boxItem=null,boxDisplay=null,elemIndex=null,elemParent=null;
//初始化展開(kāi)第一個(gè)
for(var i=0; i < listBox.length;i++){
iBase.GetByClass('box','div',listBox[i])[0].style.display='block';
listBox[i].getElementsByTagName('span')[0].innerHTML='-';
}
//遍歷所有點(diǎn)擊項(xiàng)
for(var i=0; i < navItem.length;i++){
navItem[i].onclick=function(){
elemParent=iBase.Parents(this,'js');//獲取當(dāng)前點(diǎn)擊所在區(qū)塊
navItem=elemParent.getElementsByTagName('h2');//獲取當(dāng)前區(qū)塊下的點(diǎn)擊項(xiàng)
icoItem=elemParent.getElementsByTagName('span');//獲取當(dāng)前區(qū)塊下的展開(kāi)關(guān)閉
boxItem=iBase.GetByClass('box','div',elemParent);//獲取需要控制的區(qū)塊
elemIndex=iBase.Index(this,navItem);//獲取當(dāng)前點(diǎn)擊在當(dāng)前區(qū)塊點(diǎn)擊項(xiàng)中的索引
//切換展開(kāi)關(guān)閉圖標(biāo)
icoItem[elemIndex].innerHTML= icoItem[elemIndex].innerHTML=='-' ? '+' : '-';
if(iBase.AttrStyle(boxItem[elemIndex],'display')=='block'){
//控制項(xiàng)展開(kāi)狀態(tài)下,隱藏當(dāng)前,展開(kāi)其他的第一項(xiàng)
//此處有個(gè)展開(kāi)0/1的判斷,因?yàn)楫?dāng)點(diǎn)擊第一個(gè)時(shí)是不能再展開(kāi)第一個(gè)的
boxItem[elemIndex].style.display='none';
if(elemIndex==0){
boxItem[1].style.display='block';
icoItem[1].innerHTML='-'
}else{
boxItem[0].style.display='block'
icoItem[0].innerHTML='-'
}
}else{
//控制項(xiàng)展開(kāi)狀態(tài)下,展開(kāi)當(dāng)前,隱藏其他項(xiàng)
boxItem[elemIndex].style.display='block';
for(var k=0;k < boxItem.length; k++){
if(k!=elemIndex){
boxItem[k].style.display='none';
icoItem[k].innerHTML='+';
}
}
}
}
}
}
//jQuery版本 ***** start
$(function(){
//變量定義區(qū)
var _listBox=$('.jq');
var _navItem=$('.jq>h2');
var _boxItem=null, _icoItem=null, _parents=null, _index=null;
//初始化第一個(gè)展開(kāi)
_listBox.each(function(i){
$(this).find('div.box').eq(0).show();
$(this).find('h2>span').eq(0).text('-');
});
//遍歷所有的點(diǎn)擊項(xiàng)
_navItem.each(function(i){
$(this).click(function(){
//找到當(dāng)前點(diǎn)擊父元素為listbox(單個(gè)區(qū)塊)的元素
_parents=$(this).parents('.listbox');
_navItem=_parents.find('h2');//此區(qū)塊中的點(diǎn)擊項(xiàng)
_icoItem=_parents.find('span');//此區(qū)塊中的展開(kāi)關(guān)閉圖標(biāo)
_boxItem=_parents.find('div.box');//此區(qū)塊中展開(kāi)關(guān)閉項(xiàng)
_index=_navItem.index(this);//取得當(dāng)前點(diǎn)擊在當(dāng)前區(qū)塊下點(diǎn)擊項(xiàng)中的索引值
if(_boxItem.eq(_index).is(':visible')){
//若當(dāng)前點(diǎn)擊項(xiàng)下的展開(kāi)關(guān)閉項(xiàng)是顯示的,則關(guān)閉,同時(shí)展開(kāi)另外項(xiàng)中的第一個(gè)
_boxItem.eq(_index).hide().end().not(':eq('+_index+')').first().show();
_icoItem.eq(_index).text('+').end().not(':eq('+_index+')').first().text('-');
}else{
//若當(dāng)前點(diǎn)擊項(xiàng)下的展開(kāi)關(guān)閉項(xiàng)是隱藏的,則展開(kāi),同時(shí)隱藏其他項(xiàng)
_boxItem.eq(_index).show().end().not(':eq('+_index+')').hide();
_icoItem.eq(_index).text('-').end().not(':eq('+_index+')').text('+');
}
});
});
});
演示地址:http://demo.jb51.net/js/jsjq-flod-onlyone/index.htm
打包下載:http://www.gxlcms.com/jiaoben/33950.html
本人來(lái)自Mr.Think的博客 http://mrthink.net/jsjq-flod-onlyone/
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com