如何在页面刷新时保持 selected/active HTML link 颜色?
How to keep selected/active HTML link color on page refresh?
编辑:
我想在页面刷新时保持选中状态保持HTMLlink颜色。我尝试了其他已经得到解答但对我不起作用的问题。
例如:
当我点击 HTML link 分类 HTML link 测验 时,它会将颜色更改为红色,并且保持页面刷新时的颜色。
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;
}
这是应该在 DOM 就绪时触发的 JS:
$('#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();
这是 HTML;添加了 data-
属性和 .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
更新
至于.focus
class的操作,一些额外的JS就足够了。无需在 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
编辑:
我想在页面刷新时保持选中状态保持HTMLlink颜色。我尝试了其他已经得到解答但对我不起作用的问题。
例如:
当我点击 HTML link 分类 HTML link 测验 时,它会将颜色更改为红色,并且保持页面刷新时的颜色。
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;
}
这是应该在 DOM 就绪时触发的 JS:
$('#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();
这是 HTML;添加了 data-
属性和 .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
更新
至于.focus
class的操作,一些额外的JS就足够了。无需在 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