- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 bootstrap 3.3.7 编写了一个导航选项卡
<ul class="nav nav-tabs nav-justified">
<li><a href="#">Concept</a></li>
<li><a href="#">Code</a></li>
<li ><a href="#">Read</a></li>
<li><a href="#">Action</a></li>
</ul>
每次我单击一个选项卡时,它都会被激活,而其他选项卡则会被停用,
<script>
$(document).ready(
$(".nav-tabs li").on("click", function(){
$(".nav-tabs li").removeClass("active");
$(this).addClass("active");
}));
</script>
它可以很好地使用代码,
但是,当我刷新页面时,所有标签active
类都被删除了,
我希望它保留在点击的标签上,直到触发另一次点击。
最佳答案
您正在做的是在客户端。他们并没有坚持下去。使用 localStorage
或 Cookie 来实现持久状态,即使在页面重新加载后也是如此。
此外,下面的代码将无法工作并抛出错误。 更正后的代码是:
$(document).ready(function() {
$(".nav-tabs li").on("click", function() {
$(".nav-tabs li").removeClass("active");
$(this).addClass("active");
});
});
因此每次发生点击事件时,设置localStorage
:
$(document).ready(function() {
$(".nav-tabs li").on("click", function() {
$(".nav-tabs li").removeClass("active");
$(this).addClass("active");
localStorage.setItem("active", $(this).text().trim());
});
});
当您加载页面时,设置active
类:
$(document).ready(function() {
$(".nav-tabs li").each(function () {
if (typeof localStorage.getItem("active") != "undefined" && $(this).text().trim() == localStorage.getItem("active"))
$(this).addClass("active");
});
$(".nav-tabs li").on("click", function() {
$(".nav-tabs li").removeClass("active");
$(this).addClass("active");
localStorage.setItem("active", $(this).text().trim());
});
});
片段
$(document).ready(function() {
$(".nav-tabs li").each(function() {
if (typeof localStorage.getItem("active") != "undefined" && $(this).text().trim() == localStorage.getItem("active"))
$(this).addClass("active");
});
$(".nav-tabs li").on("click", function() {
$(".nav-tabs li").removeClass("active");
$(this).addClass("active");
localStorage.setItem("active", $(this).text().trim());
});
});
.active {font-weight: bold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs nav-justified">
<li><a href="#">Concept</a></li>
<li><a href="#">Code</a></li>
<li><a href="#">Read</a></li>
<li><a href="#">Action</a></li>
</ul>
注意: StackSnippets 不允许 localStorage
。请检查输出:JSBin .
关于jquery - 遍历导航标签并停留在其中之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287812/
这是我的代码 />100 1000 它不按要求工作.. 当提交表单时(并且在任何错误情况下)它返回到默认选中的单选按钮,即值 = 1000 用户必须再次单击值 = 100,而目标是,如果用户选择了 1
假设我有一个透明的红色 HTML 元素。当我悬停该元素时,它应该变成纯红色。当我停止悬停该元素时,它应该动画回到第一个状态,但仅在 X 秒后。 到目前为止一切顺利,请参阅代码片段。 我的问题是当我停止
我遇到了 cookie 情况,我的 cookie 会存储一个颜色名称或根本不存储任何内容。所以让我们这样解释吧。我的 cookie 与我网站的外观有关,我的网站有 3 种外观: 正常(完全没有 coo
这是我的问题。我有一张包含三个 div 的 Bootstrap v4 卡。 A 是最重要的一个,我希望它保持在左上角。 当页面较宽时,我希望 B 和 C 在 A 的右侧。 当页面变窄时,卡片缩小,C
示例表: uid time_stp traf 1 2016-01-13 00:00:00 6 1 2016-01-13 05:00:00 8 1
我是一名优秀的程序员,十分优秀!