2011-08-08 47 views
0

我有一個使用選項卡界面設置的頁面。當點擊一個選項卡,下面的函數調用:jQuery切換()錯誤

function DisplayContent(tabname, newobject) //for displaying the content in the tabs 
{ 
    document.getElementById("display").innerHTML = document.getElementById(tabname).innerHTML; //writing from a hidden div on the same page 
    if(!newobject) newobject=document.getElementById('tab'+tabname); 
    if(lastobject) lastobject.className=''; 
    newobject.className='selected'; 
    lastobject=newobject; 
} 

我用一個按鈕內的下列功能,以切換頁面上的某些元素:

onclick="$('.duedate').toggle();" 

如果我不去點擊按鈕,一切都很好。但是,如果我單擊按鈕以顯示隱藏的內容,然後重新隱藏內容,則當我更改選項卡時會出現問題 - 當我再次返回到該選項卡時,內容將返回顯示。爲什麼這些元素再次出現,即使我沒有點擊按鈕?希望我有一些道理,這是一個難以描述的問題。

回答

3

因爲您正在從dom中移除隱藏的元素並用新的HTML替換它們。如果你想保留這些樣式,你還必須保留這些元素。使用.show().hide()來顯示您的div,而不是複製html。

function DisplayContent(tabname, newobject) //for displaying the content in the tabs 
{ 
    $("tabContent").hide(); // hide all the tab content divs 
    $("#" + tabname).show(); // show just the current tab's content div 
    if(!newobject) newobject=document.getElementById('tab'+tabname); 
    if(lastobject) lastobject.className=''; 
    newobject.className='selected'; 
    lastobject=newobject; 
} 

對於這項工作,給所有的內容div的一類「tabContent」,並把它們放在你的<div id="display"></div>之後。

+0

是的,您每次將HTML複製到「display」時都會覆蓋您的HTML。 –

+0

我認爲這是可能發生的事情,我將不得不對CSS做一些更改以使其工作。謝謝! – muttley91

+0

雖然我很困惑,爲什麼內容默認顯示,而不是隱藏在這個重寫... – muttley91