2013-10-04 65 views
0

我遇到了一些JavaScript問題,我發現它是一個內容切換解決方案。Javascript內容顯示「無法設置屬性」顯示「未定義」

在網頁上:www.enyx.sk/cubecraft,當您加載頁面時,您應該獲得第一個故事或標籤「O nas」的內容,但在頁面加載後,此頁面的內容不會顯示,您必須單擊菜單鏈接,以便它顯示...

我檢查了網站加載後的代碼,並得到以下錯誤:Uncaught TypError:無法設置屬性「顯示」未定義。

只是爲了清楚我是JS的新手初學者,這是我在網上找到的內容切換解決方案。它的工作細到現在爲止,我也弄不清是什麼原因導致的第一內容隱藏...

非常感謝您的幫助

回答

0

有下面的代碼片段一個問題。因爲firstChild不會返回你期望的div。

var firstone=document.getElementById('stories').firstChild; 
if (firstone.nodeType != 1) {firstone = firstone.nextSibling;} 
firstone.style.display="block"; 
} 

而不是嘗試使用這個。

document.getElementById('stories').getElementsByClassName("story")[0].style.display= 'block' 

它將工作:-)

編輯

var firstone=document.getElementById('stories').getElementsByClassName("story")[0] 
    if (firstone.nodeType != 1) {firstone = firstone.nextSibling;} 
    firstone.style.display="block"; 
+0

感謝這個偉大工程:) –