2011-01-14 48 views
0

我遇到了一些使用getElementById的JavaScript問題。它可以在FF,Safari和Chrome中正常工作,但IE(8 - 沒有嘗試過其他)可以保留。Internet Explorer在getElementById上拋出錯誤

HTML的相關位是一個div叫topnav:

<div id="topnav"> 
    ... some HTML ... 
    <div> 
    <div id="sub_1" class="lowernav"> 
    ... some HTML ... 
    </div> 
    <div id="sub_2" class="lowernav"> 
    ... some HTML ... 
    </div> 

在我的JavaScript,我想找到topnav。完整的代碼(最多的地方撈出)是這樣的:

<script> 

window.onload = init(); 

function init() { 
    // Show current menu 
    showCurrentMenu(); 
} 

function showCurrentMenu() { 
    hideMenus(); // Hide all menus and then show the current one 
    topnav = document.getElementById('topnav'); 
    ... rest of code ... 
} 

function hideMenus() { 
    var divlist = document.getElementsByTagName('div'); 
    for(var ii=0; ii<divlist.length; ii++) { 
    if(divlist[ii].className != divlist[ii].className.replace('lowernav','')) { 
    divlist[ii].className += ' hidden'; 
    } 
    } 
} 

...然後一些其他的代碼還沒有達到...

難道我做錯了什麼嗎?這可能是一件非常明顯的事情,但對我而言,我看不到它!所有的建議非常感謝。

ETA:好吧,這裏是整個代碼,因爲它目前爲:

<script> 
window.onload = init; 

function init() { 
    // Show current menu 
    showCurrentMenu; 
    // Attach 'onmouseover' event to main menu items 
    topnav = document.getElementById('topnav'); 
    // Get all items in list 
    var menulist = topnav.getElementsByTagName('a'); 
    for(var ii=0; ii<menulist.length; ii++) { 
    menulist[ii].onmouseover = showMenu; 
    } 

    document.getElementById('mainHomeNav').onmouseout = restoreMenu; 
} 

function restoreMenu(e) { 
    var targ; 
    if (!e) var e = window.event; 
    if (e.target) targ = e.target; 
    else if (e.srcElement) targ = e.srcElement; 
    if (targ.nodeType == 3) // defeat Safari bug 
     targ = targ.parentNode; 
    if (targ.id == "mainHomeNav") { 
    showCurrentMenu; 
    } 
} 

function hideMenus() { 
    var divlist = document.getElementsByTagName('div'); 
    for(var ii=0; ii<divlist.length; ii++) { 
    if(divlist[ii].className != divlist[ii].className.replace('lowernav','')) { 
    divlist[ii].className += ' hidden'; 
    } 
    } 
} 

function showCurrentMenu() { 
    hideMenus; 
    topnav = document.getElementById('topnav'); 
    // Get all items in list 
    var menulist = topnav.getElementsByTagName('a'); 
    for(var ii=0; ii<menulist.length; ii++) { 
    if(menulist[ii].className != menulist[ii].className.replace('thisSection','')) { 
    var thisid = menulist[ii].id; 
    var thissubmenu = document.getElementById(thisid + '_sub'); 
    thissubmenu.className = thissubmenu.className.replace(/hidden/g,''); 
    } 
    } 
} 

function showMenu() { 
    hideMenus; 
    // show this menu 
    var submenu_id = this.id + '_sub'; 
    var submenu = document.getElementById(submenu_id); 
    submenu.className = submenu.className.replace(/hidden/g,''); 
} 
</script> 

回答

4

問題是

window.onload = init(); 

這將立即調用init函數,然後用它的返回值該頁面的onload函數。您需要:

window.onload = init; 

只有在頁面完全加載後纔會調用init函數。

+0

+1爲正確的答案。在嘗試調試時容易犯錯誤,容易錯過。 – Spudley 2011-01-14 15:47:10

0

你的問題出在下面一行:

window.onload = init(); // this will CALL init() and assign the return value 

由於init不返回任何東西,window.onloadundefined

現在,它不在IE中工作,但在其他瀏覽器中的原因是那些其他瀏覽器可能已經解析了DOM的一部分,因此調用showCurrentMenu的作品。

但是,你同樣可以打破的,因爲從技術角度來看,不能保證該文件被加載,要解決,你有實際功能的引用賦予window.onload做:

window.onload = init; 
+0

謝謝你,但我改變了它,它仍然給錯誤。 – Sharon 2011-01-14 15:40:04

+0

@Sharon那麼你必須向我們展示更多的代碼,因爲現在,我不能做出任何其他錯誤。 – 2011-01-14 15:41:07

2

我發現了這個問題 - 我在'topmenu'前沒有'var'。

所以不是

topnav = document.getElementById('topnav'); 

它應該是

var topnav = document.getElementById('topnav'); 

感謝大家的幫助。