current class removed when refreshing page

前端 未结 4 1527
太阳男子
太阳男子 2021-01-27 17:08

I have created a dynamic link list in one page...

When a click is done on one link on that dynamic list i want to show to user that link is active, so i will add one cla

相关标签:
4条回答
  • 2021-01-27 17:30

    Changing the class via AJAX only stores the information locally. So, whenever you refresh the page, this data is lost.

    To get around this, you could get your page to remember this by setting a cookie.

    0 讨论(0)
  • 2021-01-27 17:38

    Try to use localstorage() like,

    $(function(){
        // if localstorage activeArea is set then add activearea class to menu
        if(localStorage && localStorage.getItem('activeArea')==1){
           $('a.areamenu').addClass("activearea");
        } 
        $('a.areamenu').click(function(){
           $('a.areamenu').removeClass("activearea");
           $(this).addClass("activearea");
           localStorage.setItem('activeArea',1);// set value in localstorage
        });
    });
    
    0 讨论(0)
  • 2021-01-27 17:41

    try it with PHP script like below,

    Assuming page URl will be like: http://example.com/areas/categorynameNavLink/subcatnameNAV/123

    <?php
    $CatSelectID =  end(explode('/',curPageURL()));
    ?>
    
    <li>
    <a <?php if($subcatid == $CatSelectID) echo 'class="areamenu"';?> href="/areas/'.$categorynameNavLink.'/'.$subcatnameNAV.'/'.$subcatid.'/">'.$subcatname.'
    </a>
    </li>
    
    <?php
    function curPageURL() {
     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }
     return $pageURL;
    }
    ?>
    

    Note: not tested

    0 讨论(0)
  • 2021-01-27 17:43

    In click function use e.preventDefault()

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