简体   繁体   English

一旦按钮内的链接也处于活动状态,则将导航栏下拉按钮设置为活动状态(更改颜色)

[英]Setting a Navbar dropdown button active (Change color) once a link inside the button is also active

So I have been trying to set a Navbar dropdown button active (Change color) once a link inside the button is also active, cant seem to work out how, new to html因此,一旦按钮内的链接也处于活动状态,我一直在尝试将导航栏下拉按钮设置为活动状态(更改颜色),似乎无法弄清楚 html 的新手如何

I have set up the navbar on the top of the page with some links inside aswell as a few drop buttons.我已经在页面顶部设置了导航栏,里面有一些链接以及一些下拉按钮。 i have posted a sample of the HTML code along with the CSS.我已经发布了 HTML 代码的示例以及 CSS。

 .navbar a.active { color: red; }.navbar { overflow: hidden; background-color: #325d81; padding: 0px; }.navbar a { float: left; font-size: 21px; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; }.dropdown { float: left; overflow: hidden; }.dropdown.dropbtn { font-size: 21px; border: none; outline: none; color: white; padding: 14px 30px; background-color: inherit; font-family: inherit; margin: 0; cursor: pointer; }.navbar a:hover:not(.active), .dropdown-content a:hover:not(.active), .dropbtn { color: #B0B0B0; cursor: pointer; }.dropdown-content { display: none; position: absolute; color: #f2f2f2; background-color: ; min-width: 250px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; }.dropdown-content a { float: none; padding: 12px 16px; text-decoration: none; display: block; text-align: left; background-color: #4078a5; }.dropdown-content a:hover:not(.active) { color: #B0B0B0; }.dropdown:hover.dropdown-content { display: block; }
 <div class="navbar"> <div class="dropdown"> <div class="dropdown"> <button class="dropbtn" onclick="myFunction()">Dropdown <i class="fa fa-caret-down"></i> </button>` </ul>` <div class="dropdown-content" id="myDropdown"> <a class="active" href="Stats.html">Stats</a> <a href="Images.html">Images</a> <a href="Document.html">Document</a> </div> </div> </div>

any help would be super.任何帮助都会很棒。 i have tried looking it up.我试过查找它。

This code is for Stats.html similarly change the class name to "active" in other html files此代码适用于 Stats.html 同样在其他 html 文件中将 class 名称更改为“活动”

<a class="active" href="Stats.html">Stats</a>
<a href="Images.html">Images</a>
<a href="Document.html">Document</a>

This is for Images.html这是用于图像的。html

<a href="Stats.html">Stats</a>
<a class="active" href="Images.html">Images</a>
<a href="Document.html">Document</a>

And this is for Document.html这是针对 Document.html

<a href="Stats.html">Stats</a>
<a href="Images.html">Images</a>
<a class="active" href="Document.html">Document</a>

If you do so, then clicking the button will take you to that page and also makes that button red.如果这样做,则单击该按钮会将您带到该页面,并且该按钮也会变为红色。 You can use same css您可以使用相同的 css

The way is to create separate classes for each button.方法是为每个按钮创建单独的类。 This code works fine but the dropdown becomes horizontal.此代码工作正常,但下拉列表变为水平。

 .navbar a.active { color: red; }.navbar { overflow: hidden; background-color: #325d81; padding: 0px; }.navbar a { float: left; font-size: 21px; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; }.dropdown { float: left; overflow: hidden; }.dropdown.dropbtn { font-size: 21px; border: none; outline: none; color: white; padding: 14px 30px; background-color: inherit; font-family: inherit; margin: 0; cursor: pointer; }.navbar a:hover:not(.active), .dropdown-content a:hover:not(.active), .dropbtn { color: #B0B0B0; cursor: pointer; }.dropdown-content { display: none; position: absolute; color: #f2f2f2; background-color: ; min-width: 250px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 1; }.inactive1 { float: none; padding: 12px 16px; text-decoration: none; display: block; text-align: left; background-color: #4078a5; }.inactive2 { float: none; padding: 12px 16px; text-decoration: none; display: block; text-align: left; background-color: #4078a5; }.dropdown-content a:hover:not(.active) { color: #B0B0B0; }.active { background-color: #325d81; }.dropdown:hover.dropdown-content { display: block; }
 <div class="navbar"> <div class="dropdown"> <div class="dropdown"> <button class="dropbtn" onclick="myFunction()">Dropdown <i class="fa fa-caret-down"></i> </button>` </ul>` <div class="dropdown-content" id="myDropdown"> <a class="active" href="Stats.html">Stats</a> <a class="inactive1" href="Images.html">Images</a> <a class="inactive2" href="Document.html">Document</a> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM