li menu needs class of “selected”

前端 未结 7 1472
抹茶落季
抹茶落季 2021-01-15 18:20

when the user clicks on a menu tab i want it to remain selected with it a white button.

here is my attempt but its not working. if you click the home button it does

相关标签:
7条回答
  • 2021-01-15 18:27

    As far as I can tell from your CSS, you haven't defined any styles for the selected class.

    Assigning that class to your li isn't enough. You also need to style the class in the way you'd like.

    .selected{
     background-color:#fff;
    }
    

    (etc)

    0 讨论(0)
  • 2021-01-15 18:30

    As others stated you dont have a .selected class also your js will set all li elements to selected when one is clicked you may want to change it to this on the second line

    $('#navigation  li').click(function() {
       $(this).addClass('selected');
    });
    
    0 讨论(0)
  • 2021-01-15 18:30
    #navigation li.a.seletected a.seletected 
    {
        background: white; // or background Image or whatever it is your doing to make this      white.
    }
    
    0 讨论(0)
  • 2021-01-15 18:32

    I see several modifications here.

    Firstly, when you apply selected class, you are applying to all li items, which is not true. you only want to apply the class to clicked list item.

    secondly, when you click another list item, you want to remove the selected class.

    so the code modified would be:

    $('#navigation li').click(function() {
        $('#navigation li').removeClass('selected');
        $(this).addClass('selected');
    });
    

    Most importantly, you do not have a selected class. I put a temporary selected class definition like so:

    .selected{
    border: 1px solid #888;
    background-color: #white;
    

    }

    You can see a working example here: on JsFiddle

    additionally, your a element has a gray background. so you might want to apply selected white background class to your a element also.. something like:

    $('a', this).addClass('selected'); //apply to  anchor element also
    

    and when you remove the class, follow the same deal.

    So you want to persist the button states across different Pages. Javascript is only valid as long as the page is open. As soon as the user goes to the new page, all javascript will be reset. To overcome this you can do one of these two things:

    1) If you are using a master page for menu, add runat="server" attribute to your list items, and from code behind, add selected class to appropriate list item from your child page (may be you could have a public function in your Master page)

        //Master page
        public SetHomeMenu()
        {
            liHome.Attributes.Add("class","selected");
        }
    
        //in Child page
        Master.SetHomeMenu(); 
    

    2) If you want to do it in javascript, you need to read your url, parse it, then add selected class to appropriate li

    //put this javascript in your head section or even at the bottom, just before closing
    // body tag </body>
    
        $(document).ready(function () {
            if(window.location.href.indexOf("home"))
            {
                 $("#navigation li#home").addClass("selected");
            }
            else if(window.location.href.indexOf("about"))
            {
                 $("#navigation li#about").addClass("selected");
            }
            else if(window.location.href.indexOf("contact"))
            {
                 $("#navigation li#contact").addClass("selected");
            } 
    
        });
    

    But for this to work, you need to add id attributes to your list items like so:

    <ul id="navigation">
        <li id="home"><a href="#"><span>HOME</span></a></li>
        <li id="about"><a href="/en-us/about.aspx"><span>ABOUT</span></a></li>
        <li id="contact"><a href="/en-us/contact.aspx"><span>CONTACT</span></a></li>
    </ul>
    

    For use the last solution you need to change the if statement to this example:

    if(window.location.href.indexOf("home") > -1)

    0 讨论(0)
  • 2021-01-15 18:33

    This'd do it. You forgot to set the selected class css

    http://fiddle.jshell.net/54uDQ/

    The important part is this css

    #navigation a:hover, #navigation a.selected
    {
    
        background: url('http://i51.tinypic.com/2iih9c9.png') no-repeat scroll 0 0 transparent;
        color: #000000;
        height: 43px;
        list-style: none outside none;
        padding-left: 10px;
        text-decoration: none;
        width: 116px;
        text-align:center
    }
    
    0 讨论(0)
  • 2021-01-15 18:42

    You haven't even set a class for 'selected' in your CSS.

    Add this and it should work.

    #navigation li .selected {
       [CSS to make white background here.]
    }
    
    0 讨论(0)
提交回复
热议问题