kap*_*ffy 2 css jquery menu selected
我从另一个问题中抓住了这个片段:
<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>
Run Code Online (Sandbox Code Playgroud)
我想要的最后一件事是在单击另一个选项卡时恢复选项卡普通css.例如,当我点击tab1时,标签的bgcolors为白色.当我进入Tab2时,它变为黑色.Tab1变为白色,Tab2变为黑色
<ul>
<li>
<a href="#Tab1">Tab 1</a>
</li>
<li>
<a href="#Tab2">Tab 2</a>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
让我们说这是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;}
Run Code Online (Sandbox Code Playgroud)
实际上,你可以大大简化你的Javascript.这应该达到你想要的效果.
<script type="text/javascript">
$(document).ready(function() {
$("div.content ul li a")
.mouseover(function() {
$(this).addClass('mouseover');
})
.mouseout(function() {
$(this).removeClass('mouseover');
});
$("div.content ul li a").click(function(e) {
e.preventDefault(); //prevent the link from actually navigating somewhere
$(this).toggleClass("clicked");
$("div.content ul li a").not(this).removeClass("clicked"); //remove the clicked class from all other elements
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
这里的Javascript将执行以下操作: