[英]How can I override an anchor element's font color when applying an active class to its parent li element?
I have a navigation bar with three buttons which I'm trying to highlight when they're active by applying "active" class to the li
elements using jQuery - the border and font color are supposed to change. 我有一个带有三个按钮的导航栏,我想通过使用jQuery将“ active”类应用于li
元素来突出显示它们处于活动状态时的方式-边框和字体颜色应该会改变。 So far, only the border color changes while the text remains the same. 到目前为止,只有边框颜色会更改,而文本保持不变。
Also, I'd like to override or prevent the :hover
pseudo class when the link is active. 另外,当链接处于活动状态时,我想覆盖或阻止:hover
伪类。
Here's the codepen which will hopefully make this clearer. 这是希望能使这一点更加清晰的代码笔 。 Could you please advise how to override the a
element's font color? 能否请你建议如何重写a
元素的字体颜色?
Thanks! 谢谢!
You have HTML looking like 您的HTML看起来像
<ul class="topnav">
<li class="topnavli">
<a href="#fun-stop">Link 3</a>
</li>
.......
Then styles for the anchors 然后锚的样式
.topnav a { color: #fff; }
then you add the active class to the LI elements, but the styles are only set as 然后将活动类添加到LI元素,但是样式仅设置为
.active { color: #FFADA0; }
That's just styling the LI element, not the anchors inside, and the specification states that by default an anchor tag does not inherit attributes like color from the parent element if a href
attribute is present, so those styles must be set directly on the anchor with something like 这只是对LI元素(而不是内部的锚)进行样式设置,并且规范指出,默认情况下,如果存在href
属性,则锚标签不会从父元素继承颜色之类的属性,因此,必须使用就像是
li.active a { color: #FFADA0; }
As the border is set on the LI element, that should be changed with a specific style for that element, so you end up with 由于在LI元素上设置了边框,因此应使用该元素的特定样式进行更改,因此最终得到
li.active {
border:3px solid #FFADA0;
}
li.active a {
color: #FFADA0;
}
Add 加
.topnav .active a {
color: #FFADA0 ;
}
$('.topnav li').click(function() { $(".topnav li.active").removeClass("active"); $(this).addClass('active'); });
.header h1 { font-family: 'Montserrat', sans-serif; font-size: 44px; letter-spacing: 3px; margin: 0; padding: 15px 0 15px 30px; display: inline-block; color: #fff; } .topnav { position: fixed; top: 0; width: 100%; list-style: none; font-family: 'Montserrat', sans-serif; background-color: #000; z-index: 1; } .topnav li { display: block; float: right; margin: 20px 8px 0 0; } .topnavli { border: 3px solid #fff; } .topnav li:first-child { margin-right: 50px; } .topnav li:hover { border: 3px solid #6fb7b7; transition: 0.3s; } .topnav .active a { color: #FFADA0 ; } .topnav a { display: block; color: #fff ; font-weight: 600; padding: 10px 0 ; font-family: 'Raleway', sans-serif; text-align: center; text-decoration: none; width: 120px; } .topnav a:hover { color: #6fb7b7; text-decoration:none; transition: 0.3s; } .active { color: #FFADA0 ; border:3px solid #FFADA0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="header"> <ul class="topnav"> <li class="topnavli"> <a href="#fun-stop">Link 3</a> </li> <li class="topnavli"><a href="#projects">Link 2</a></li> <li class="topnavli"><a href="#home">Link 1</a></li> <h1>website</h1> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.