0

I searched through many links but could not find a suitable solution, i want to highlight the selected list in my web page. my code for list is:

%div#navg
  %ul.clearfix
    %li
      %a#c-realtime.overview-16{:href => "#", :title => "Summary"} Summary
    %li.menu-trigger
      %a#c-comment.events-24{:href => events_url, :title => "Events"} Events
    %li.menu-trigger
      %a#c-realtime.create-24{:href => new_event_path, :title => "Create new event"} Create new event 
    %li.menu-trigger
      %a#c-realtime.realtime-16{:href => "#", :title => "Analytics"} Analytics
    %li.menu-trigger
      %a#c-realtime.recommend-24{:href => recomendations_url, :title => "Recommendations"} Recommendations

and code for java script is:

:javascript
$( document ).ready( function() {
    $( "#navg ul li a" ).click( function() {
        $( this ).parent( "li" )
            .addClass( "selected" )
            .siblings( ".selected" )
                .removeClass( "selected" );
        return false;
    });
});

and in css i am using this:

li.selected { background-color: #404649; 

my problem is i am able to highlight the selected menu in my page but the respective links are not working, when i remove the line

return false;

from my JS code my links are working but now it is not highlighting the link, I am not able to figure out the problem please suggest me how to solve my problem. Thank u

4

2 回答 2

0

那这个呢?

$( document ).ready( function() {
    $( "#navg ul li a" ).click( function(e) {
        e.preventDefault();  //  prevent following the link on the first click
        $( this ).parent( "li" )
            .addClass( "selected" )
            .siblings( ".selected" )
                .removeClass( "selected" );
        window.location.href = $( this ).attr('href');  // manually follow the link
    });
});
于 2012-05-03T07:03:34.963 回答
0

usingreturn false;取消事件生成的默认行为。此外,javascript 会更新加载页面上的内容。它不会影响新页面上的行为。

这就是为什么当你使用return false;你的链接不起作用的原因。当你删除return false;你的链接时不要装饰

相反,请尝试在链接页面的 javascript 中应用这些类。

于 2012-05-03T07:10:01.387 回答