简体   繁体   中英

How to keep selected/active HTML link color on page refresh?

EDIT :

I want to keep selected keep HTML link color on page refresh . I tried other problems that already been answered but didn't work on me.

For example:

When I clicked HTML link Categories the HTML link Quizzes it will change the color to red, and keep the color on page refresh.

HTML

<div id="col-navigation">
        <ul>
            <li> 
                <a href="#"> Quizzes </a>
            </li>
            <li> 
                <a href="#"> Categories </a>
            </li>
            <li> 
                <a href="#"> Jump </a>
            </li>
        </ul>
</div>

<div id="quizzes"> 
     Quizzes! <!-- default showed -->
</div>

<div id="categories" style="display:none">
     Categories! <!-- I have table here, plain text is just a example -->
</div>

 <div id="jump" style="display:none">
     Jump! <!-- I have table here, plain text is just a example -->
</div>

JS/JQUERY:

//SHOW AND HIDE
    $('#col-navigation a').click(function(){
   $('#quizzes,#categories,#jump').show().not('#' + $.trim($(this).text()).toLowerCase()).hide();
});

//STAY THE COLOR OF ACTIVE LINK
$('li').click(function() {
  $(this).siblings().find('a').removeClass('focus');
  $(this).find('a').addClass('focus');
});

CSS

    li a {
  padding: 20px;
  display: block;
}

li a:hover {
  background-color: white;
}

.focus {
  background-color: lightblue;
  color: red;
}

li a {
  color: inherit;
}

Here's the JS which should be triggered on DOM ready:

$('#col-navigation a').click(function(e){
   e.preventDefault();
   $(".content").hide().filter( $(this).data("target") ).show();
   localStorage.setItem('target', $(this).data("target"));
});
var target = localStorage.getItem('target');
!target || $('.content').hide().filter(target).show();

And here's the HTML; added data- attribute and .content class:

<div id="col-navigation">
        <ul>
            <li> 
                <a href="#" data-target="#quizzes"> Quizzes </a>
            </li>
            <li> 
                <a href="#" data-target="#categories"> Categories </a>
            </li>
            <li> 
                <a href="#" data-target="#jump"> Jump </a>
            </li>
        </ul>
</div>

<div id="quizzes" class="content"> 
     Quizzes! <!-- default showed -->
</div>

<div id="categories" style="display:none" class="content">
     Categories!
</div>

 <div id="jump" style="display:none" class="content">
     Jump!
</div>

Demo Using localStorage to remember last selected option

UPDATE

As for manipulation of the .focus class, some additional JS will suffice. No need to save any additional info in localStorage .

$('#col-navigation a').click(function(e){
   e.preventDefault();
   $(this).addClass('focus').parent().siblings().find('a').removeClass('focus'); //<<<--THIS
   $(".content").hide().filter( $(this).data("target") ).show();
   localStorage.setItem('target', $(this).data("target"));
});
var target = localStorage.getItem('target');
!target || $('.content').hide().filter(target).show();
!target || $('a[data-target="' + target + '"]').addClass('focus').parent().siblings().find('a').removeClass('focus'); //<<<-- AND THIS

Updated Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM