更改所选菜单选项卡的颜色
我从另一个问题中获取了这个片段:
<script type='text/javascript' >
$(document).ready(function () {
$("div.content ul li a")
.mouseover(function () {
var t = $(this);
if (!t.hasClass("clicked")) { // very easy to check if element has a set of styles
t.addClass('mouseover');
}
})
.mouseout(function () { // attach event here instead of inside mouse over
$(this).removeClass('mouseover');
});
$("div.content ul li a").click(function () {
var t = $(this);
t.toggleClass("clicked");
if (t.hasClass("clicked")) {
t.removeClass('mouseover');
} else {
t.addClass('mouseover');
}
});
});
</script>
我想要的最后一件事是在单击另一个选项卡时恢复选项卡正常的 css。 例如,当我单击 tab1 时,选项卡的背景颜色为白色,当我进入 Tab2 时,选项卡的背景颜色变为黑色。Tab1 变为白色,Tab2 变为黑色,
<ul>
<li>
<a href="#Tab1">Tab 1</a>
</li>
<li>
<a href="#Tab2">Tab 2</a>
</li>
</ul>
假设这是 CSS 部分
ul li a {background-color: white;}
ul li a.mouseover {background-color: black;}
ul li a.mouseout {background-olor: white;}
ul li a.clicked {background-color: black;}
I grabbed this snippet from another question:
<script type='text/javascript' >
$(document).ready(function () {
$("div.content ul li a")
.mouseover(function () {
var t = $(this);
if (!t.hasClass("clicked")) { // very easy to check if element has a set of styles
t.addClass('mouseover');
}
})
.mouseout(function () { // attach event here instead of inside mouse over
$(this).removeClass('mouseover');
});
$("div.content ul li a").click(function () {
var t = $(this);
t.toggleClass("clicked");
if (t.hasClass("clicked")) {
t.removeClass('mouseover');
} else {
t.addClass('mouseover');
}
});
});
</script>
The last thing I wanted is to restore the tabs normal css when another tab is clicked.
For example, the tab's bgcolors are white when I click tab1 it becomes black when I go into Tab2..Tab1 goes white and Tab2 goes black
<ul>
<li>
<a href="#Tab1">Tab 1</a>
</li>
<li>
<a href="#Tab2">Tab 2</a>
</li>
</ul>
let's say here's the CSS part
ul li a {background-color: white;}
ul li a.mouseover {background-color: black;}
ul li a.mouseout {background-olor: white;}
ul li a.clicked {background-color: black;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您可以为此大大简化 Javascript。这应该会达到你想要的效果。
这里的 Javascript 将执行以下操作:
You can actually greatly simplify your Javascript for this. This should achieve your desired effect.
The Javascript here will do the following:
@wsanville
双击同一个选项卡的问题怎么样?
如果我在单击选项卡时从选项卡(指示选定的选项卡)中删除底部边框,那就没问题了。但是当我再次单击它时,它应该保持这样(没有底部边框),但由于现在切换,看起来您尚未选择该选项卡,但您仍然在那里。
@wsanville
What about the problem of double-clicking the same tab?
If i remove a bottom-border from a tab (indicating the selected one) when clicked on a tab, that's fine. But when i click it again, it should just stay like that (no bottom border), but because of the toggle now it looks like you haven't selected the tab but you are still there.
仅使用 CSS 即可实现您想要的效果:
演示
It is possible to achieve what you are looking for using just CSS:
Demo