2013-02-08 70 views
1

我試圖每次更改URL時更新我的​​菜單按鈕的狀態。下面的代碼是在我的按鈕mouseleaveURL更改後更改按鈕的狀態

$(document).ready(function() { 
    $("#btnAdd, #btnEdit").mouseleave(function(event) { 
     ActivateMenuButton(); 
    });  
}); 

function ActivateMenuButton() { 
    var currentURL = window.location.pathname; 
    //alert(currentURL); 

    //deselect current selected button 
    $("#btnAdd, #btnEdit").removeClass("buttonSelected"); 

    //select menu button according to URL 
    if (currentURL.indexOf('Add') >= 0) { 
     $("#btnAdd").addClass("buttonSelected");  
    } 

    if (currentURL.indexOf('Ddit') >= 0) { 
     $("#btnEdit").addClass("buttonSelected"); 
    } 
} 

有過的jQuery或javescript一種方法來檢測時,頁面的變化和射擊上面的代碼?

+1

您可以嘗試使用卸載事件:http://docs.jquery.com/Events/unload – cheesemacfly 2013-02-08 17:03:32

回答

1

你會尋找jQuery.ready(),當頁面加載完成時會觸發它。您的JavaScript代碼在頁面加載時不記得自己,因此mouseleave事件毫無意義,您只需繼續並直接激活ActivateMenuButton。

jQuery(document).ready(function() { ActivateMenuButton(); });  
+0

當然,我在想什麼?謝謝。我在盒子外面思考,問題是我在錯誤框外思考:-) – user1536396 2013-02-08 17:19:11