0

現在のページを強調したい。このコードを試しましたが、機能していません。どんな助けでもありがたいです。

$(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>
4

4 に答える 4

2

それから始めるべきだと思います:

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

URL に表示されない場合、このスクリプトを改善して、既定のページ (Home_Page.aspx になると思います) で正しく動作するようにします。

于 2012-10-03T20:49:35.813 に答える
0

これを試して

$(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 にレンダリングされていた可能性があります。

于 2012-10-03T20:34:13.157 に答える
0

ここで実際の例を示すフィドルを作成しました。

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");
    });
});​
于 2012-10-03T20:37:28.507 に答える
-1

また、試すことができます

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

あなたの内側$(document).ready(function(){});

于 2012-10-03T20:45:45.267 に答える