美好的一天
我想知道是否可以设置选定的 * li *元素的样式,就像将鼠标悬停在样式上一样?或者您是否必须使用Javascript为所选项目分配“活动”类并将其设置为样式?
HTML
<div id="areas" class="span2">
<ul>
<li><a href="#pretoriaDetail">Pretoria</a>
</li>
<li><a href="#potchDetail">Potch</a>
</li>
<li><a href="#jhbDetail">JHB</a>
</li>
</ul>
</div>
<div class="span12">
<div class="hidden" id="pretoriaDetail">Pretoria Content</div>
<div class="hidden" id="potchDetail">Potch Content</div>
<div class="hidden" id="jhbDetail">JHB Content</div>
</div>
CSS:
#areas {
margin: 0 auto;
text-align: center;
width: 100px;
}
#areas ul {
list-style: none;
}
#areas ul li {
width: 100%;
height: 100%;
background: #073640;
border: 1px solid #aaa;
margin: 10px 0px;
padding: 20% 10%;
/*to vertically center text inside the ul*/
text-align: center;
}
#areas ul li:hover {
background: #610052;
}
#areas ul li a {
font-family:'Open Sans', 'Century Gothic', sans-serif;
font-size: 28px;
color: #fff;
line-height: 100%;
width: 100%;
height: 100%;
}
#areasMap {
}
.hidden {
display: none;
}
查看我的fiddle
如果你看看我的小提琴,悬停类工作正常,但我想在你选择它时保持紫色的元素 - 我可以使用纯CSS还是我必须使用JS?
感谢
答案 0 :(得分:1)
是的,您需要在单击时添加一个类(例如.active)。它将在所有浏览器中得到认可
JS
$(function () {
$("#areas ul a").click(function (event) {
event.preventDefault();
$("#areas ul li").removeClass('active'); //clears the active class for elements
$(this).parent().addClass('active'); //adds the class to clicked li
$(".hidden").hide(500);
$($(this).attr("href")).show(500)
});
});
CSS
#areas ul li:hover,
#areas ul li.active {
background: #610052;
}
答案 1 :(得分:1)