add active class to li from page url

后端 未结 4 975
终归单人心
终归单人心 2020-12-11 13:10

I am using the following to get the active page url:



        
相关标签:
4条回答
  • 2020-12-11 13:50

    I am not sure if you want solution in Javascript. Here my suggestion Your Html code

      <ul id="navigation">
        <li class="current">
          <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=109">Link 1</a>
       </li>
       <li>
         <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=110">Link 2</a>
       </li>
       <li>
         <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=111">Link 3</a>
       </li>
     </ul>
    

    Javascript Code

    url = 'http://www.domain-nane.com/index.php?option=com_content&view=article&id=6&Itemid=111';
       //  url = '<php echo curPageURL();?>';
    var urlRegex =/Itemid=([0-9]+)/i;
    var Itemid = url.match(urlRegex)[1];
    // alert(Itemid);
    
    var nodes = document.getElementById('navigation').childNodes;
    var lis;
    for(i=0; i<nodes.length; i++) {
        if(nodes[i].nodeType == 1){
          var lis = nodes[i];
          var aElem =   lis.childNodes[1];
          var AItemId = aElem.href.match(urlRegex)[1];
        if(Itemid == AItemId) {
            lis.className = lis.className + " current";
        }
        }
    }
    

    Running Example http://jsfiddle.net/xdsUm/3/

    0 讨论(0)
  • 2020-12-11 13:54

    its simple

    <li class="<?php if($Itemid == $_REQUEST['Itemid']) { echo 'active'; } ?> ">
    

    This will look like this is in HTML

    <li class="active">
    
    0 讨论(0)
  • 2020-12-11 13:57

    you are doing wrong thing

    simple way is below..

    In your URL Itemid is unique, so its better to do this in following way

    <li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
       <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=109">Link 1</a>
    </li>
    
    <li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
       <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=110">Link 2</a>
    </li>
    
    <li <?php if($Itemid == $_REQUEST['Itemid']) { echo class='current'; } ?> >
       <a href="index.php?option=com_content&amp;view=article&amp;id=6&amp;Itemid=111">Link 3</a>
    </li>
    
    0 讨论(0)
  • 2020-12-11 14:07

    I think that you want something like this:

     <ul id="navigation">
        <li <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?>><a href="index.php">Home Page</a></li>
        <li <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?>><a href="about.php">About Me</a></li>
        <li <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?>><a href="contact.php">Contact Me</a></li>
        <li <?php if (strpos($_SERVER['PHP_SELF'], 'portfolio.php')) echo 'class="current"';?>><a href="portfolio.php">My Work</a></li>
     </ul>
    

    I hope this will help you!

    PS: index.php, about.php, contact.php and portofolio.php must be replaced with your pages links.

    0 讨论(0)
提交回复
热议问题