简体   繁体   English

将活动类应用于其父li元素时,如何覆盖锚元素的字体颜色?

[英]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;     
}

Codepen 码笔

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.

相关问题 当锚标记下方有ap元素时,如何使锚标记填充li元素 - How can I make an anchor tag fill an li element when there is a p element right underneath the anchor tag 如何确定li元素中的哪个锚是活动的 - how to determine which anchor in li element is active 如何在过滤时向li元素添加活动颜色? - How to append active color to the li element on filtering? 单击下一个/上一个按钮时,如何将活动类更改为LI元素? - How do I change the active class to the LI element when I click next/previous button? Vue JS - 如何滚动(焦点)到“活动”li 元素? - Vue JS - How can i scroll (focus) to the 'Active' li element? 如何在不对父元素应用更改的情况下将 hover class 应用于任何元素? - How can I able to apply hover class to any element without applying changes to parent elements? 如何从嵌套在li元素中的锚元素获取值 - How can I fetch value from anchor element nested in li element 如何使用 javascript 访问我的父 li 元素? - How can i access my parent li element using javascript? 如何使锚的整个父元素可点击? - How can I make the entire parent element of an anchor clickable? 当单击lielment时,如何更改jQuery中li元素上的活动类并将用户重定向到指定页面? - How to change active class on li element in JQuery and redirect the user to the specified page when li elelment is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM