簡體   English   中英

當前頁面在aspx菜單中突出顯示

[英]Current page highlighting in menu in aspx

我要突出顯示當前頁面。 嘗試過此代碼,但不起作用..任何幫助將不勝感激和贊賞。

$(function() {
    $("#site-menu li a").click(function() {
        $(".current").removeClass("current");
        //"this" is the element clicked on
        $(this).addClass("current");
    });
});​

<div id="menu">
     <ul id="site-menu">
          <li><a href="Home_Page.aspx">home</a></li>
          <li><a href="Services.aspx">services</a></li>
          <li><a href="About_Us.aspx">about us</a></li>
          <li><a href="Contact_Us.aspx">contact us</a></li>
     </ul>
</div>

我認為您應該從此開始:

$(function() {
    var curPage = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
    $("#site-menu li a[href='" + curPage + "']").addClass("current");
});​

只要改進此腳本,使其在URL中不顯示默認頁面(我認為它將是Home_Page.aspx)即可正確使用。

嘗試這個

$(function() {
    $("#site-menu li a").click(function(e) {
        e.preventDefault();
        $(".current").removeClass("current");
        //"this" is the element clicked on
        $(this).addClass("current");
    });
});​

還要確保CSS FIDDLE中.current

當我使用e.preventDefault();時似乎正常工作。

也許該類在回發時被渲染回默認的HTML。

我在這里創建了一個小提琴,顯示了一個有效的示例。

http://jsfiddle.net/psDBP/

試試這個(也請參閱我的評論):

$(function() {
    // change to on rather than deprecated click
    $("#site-menu li a").on("click", function(e) { 
        e.preventDefault(); // stops the a href firing
        $(".current").removeClass("current");
        //"this" is the element clicked on
        $(this).addClass("current");
    });
});​

您也可以嘗試

    $("#site-menu li a").live('click',function() {
        $(".current").removeClass("current");
        //"this" is the element clicked on
        $(this).addClass("current");
    });

在您的$(document).ready(function(){})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM