How to dynamically change the color of the selected menu item of a web page?

后端 未结 8 1422
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 14:55

I am new to developing web pages. I am looking to create menus similar to the ones in stackoverflow.com (like Questions, Tags, Users shown above). How do I change the color

相关标签:
8条回答
  • 2020-12-13 15:12

    At last I managed to achieve what I intended with all your help and the post Change a link style onclick. Here is the code for that. I used JavaScript for doing this.

    <html>
        <head>
            <style type="text/css">
                .item {
                    width:900px;
                    padding:0;
                    margin:0;
                    list-style-type:none;
                }
    
                a {
                    display:block;
                    width:60;
                    line-height:25px; /*24px*/
                    border-bottom:1px  none #808080;
                    font-family:'arial narrow',sans-serif;
                    color:#00F;
                    text-align:center;
                    text-decoration:none;
                    background:#CCC;
                    border-radius: 5px;
                    -webkit-border-radius: 5px;
                    -moz-border-radius: 5px;
                    margin-bottom:0em;
                    padding: 0px;
                }
    
                a.item {
                    float:left;        /* For horizontal left to right display. */
                    width:145px;       /* For maintaining equal  */
                    margin-right: 5px; /* space between two boxes. */
                }
    
                a.selected{
                    background:orange;
                    color:white;
                }
            </style>
        </head>
        <body>
            <a class="item" href="#" >item 1</a>
            <a class="item" href="#" >item 2</a>
            <a class="item" href="#" >item 3</a>
            <a class="item" href="#" >item 4</a>
            <a class="item" href="#" >item 5</a>
            <a class="item" href="#" >item 6</a>
    
            <script>
                var anchorArr=document.getElementsByTagName("a");
                var prevA="";
                for(var i=0;i<anchorArr.length;i++)
                {
                    anchorArr[i].onclick = function(){
                        if(prevA!="" && prevA!=this)
                        {
                            prevA.className="item";
                        }
                        this.className="item selected";
                        prevA=this;
                    }
                }
            </script>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-13 15:15

    Set the styles for class active and hover:


    Than you need to make the li active, on the server side. So when you are drawing the menu, you should know which page is loaded and set it to:

     <li class="active">Question</li>
     <li>Tags</li>
     <li>Users</li>
    

    But if you are changing the content without reloading, you cannot change set the active li element on the server, you need to use javascript:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <style>
      .menu{width: 300px; height: 25; font-size: 18px;}
      .menu li{list-style: none; float: left; margin-right: 4px; padding: 5px;}
      .menu li:hover, .menu li.active {
            background-color: #f90;
        }
    </style>
    </head>
    <body>
    
    <ul class="menu">
    <li>Item 1</li>
    <li class="active">Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
    <li>Item 6</li>
    </ul>
    
    <script type="text/javascript">
    
    var make_button_active = function()
    {
      //Get item siblings
      var siblings =($(this).siblings());
    
      //Remove active class on all buttons
      siblings.each(function (index)
        {
          $(this).removeClass('active');
        }
      )
    
    
      //Add the clicked button class
      $(this).addClass('active');
    }
    
    //Attach events to menu
    $(document).ready(
      function()
      {
        $(".menu li").click(make_button_active);
      }  
    )
    
    </script>
    </body>
    
    </html>
    
    0 讨论(0)
  • 2020-12-13 15:15

    Try this. It holds the color until another item is clicked.

    <style type="text/css">
    
    .activeElem{
     background-color:lightblue
    }       
    .desactiveElem{
     background-color:none
    }       
    
    }
    
    </style>
    
    <script type="text/javascript">
    var activeElemId;
    function activateItem(elemId) {
     document.getElementById(elemId).className="activeElem";
     if(null!=activeElemId) {
       document.getElementById(activeElemId).className="desactiveElem";
     }
     activeElemId=elemId;
    
    }
    </script>
    
    <li id="aaa"><a href="#" onclick="javascript:activateItem('aaa');">AAA</a>
    <li id="bbb"><a href="#" onClick="javascript:activateItem('bbb');">BBB</a>
    <li id="ccc"><a href="#" onClick="javascript:activateItem('ccc');">CCC</a>
    
    0 讨论(0)
  • 2020-12-13 15:17

    I use PHP to find the URL and match the page name (without the extension of .php, also I can add multiple pages that all have the same word in common like contact, contactform, etc. All will have that class added) and add a class with PHP to change the color, etc. For that you would have to save your pages with file extension .php.

    Here is a demo. Change your links and pages as required. The CSS class for all the links is .tab and for the active link there is also another class of .currentpage (as is the PHP function) so that is where you will overwrite your CSS rules. You could name them whatever you like.

    <?php # Using REQUEST_URI
        $currentpage = $_SERVER['REQUEST_URI'];?>
        <div class="nav">
            <div class="tab
                 <?php
                     if(preg_match("/index/i", $currentpage)||($currentpage=="/"))
                         echo " currentpage";
                 ?>"><a href="index.php">Home</a>
             </div>
             <div class="tab
                 <?php
                     if(preg_match("/services/i", $currentpage))
                         echo " currentpage";
                 ?>"><a href="services.php">Services</a>
             </div>
             <div class="tab
                 <?php
                     if(preg_match("/about/i", $currentpage))
                         echo " currentpage";
                 ?>"><a href="about.php">About</a>
             </div>
             <div class="tab
                 <?php
                     if(preg_match("/contact/i", $currentpage))
                         echo " currentpage";
                 ?>"><a href="contact.php">Contact</a>
             </div>
         </div> <!--nav-->
    
    0 讨论(0)
  • 2020-12-13 15:23

    There is a pure CSS solution I'm currently using.

    Add a body ID (or class) identifying your pages and your menu items, then use something like:

    HTML:

    <html>
        <body id="body_questions">
            <ul class="menu">
                <li id="questions">Question</li>
                <li id="tags">Tags</li>
                <li id="users">Users</li>
            </ul>
            ...
        </body>
    </html>
    

    CSS:

    .menu li:hover,
    #body_questions #questions,
    #body_tags      #tags,
    #body_users     #users {
        background-color: #f90;
    }
    
    0 讨论(0)
  • 2020-12-13 15:27

    Assuming you want to change the colour of the currently selected link/tab... you're best bet is to apply a class (say active) to the currently selected link/tab and then style this differently.

    Example style could be:

    li.active, a.active {
      background-color: #f90;
    }
    
    0 讨论(0)
提交回复
热议问题