gpt4 book ai didi

html - 如何在访问/激活子元素时更改父元素的颜色

转载 作者:太空宇宙 更新时间:2023-11-03 19:28:31 26 4
gpt4 key购买 nike

我有一个下拉导航。我想要做的是当我点击任何子菜单链接时,我希望父按钮的颜色在链接处于事件状态时保持更改。这是代码的 fiddle 链接。 https://jsfiddle.net/v28ydshL/

<nav>
<div class="navbar-header">
<button style="background-color: #3382d5;" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#menubar">
<span style="background-color: white;" class="icon-bar"></span>
<span style="background-color: white;" class="icon-bar"></span>
<span style="background-color: white;" class="icon-bar"></span>
</button>
</div>
<div id="menubar" class=" collapse navbar-collapse row">
<ul id="dropdownNavbar" class="nav navbar-nav">
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Time Card
<span class="caret"></span></button>
<div class="dropdown-content">
<a id="showTimeCard" href="#">My Time Card</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> Project
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> HR Links
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-screenshot"></span> Leave Tracker
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-briefcase"></span> Accounts
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-headphones"></span> Service Desk
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-file"></span> Reports
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-cog"></span> Settings
<span class="caret"></span></button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
</div>
</li>
</ul>
</div>
</nav>

这是我的CSS

ul {
list-style-type: none;
padding-left: 1px;

}
.dropdown {
float: left;
padding-right:1px;
}
.dropbtn{
background-color: #060975;
height:30px;
border-radius: 0px 10px;
}
li button, .dropbtn {
display: inline-block;
background-color:#3382d5;
color: white;
text-align: center;
}
li button:hover, .dropdown:hover .dropbtn {
background-color: #31b0d5;
}
button.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #e8f3f4;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.1);
z-index: 10;
}
.navbar-header{
overflow: visible;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 5px 10px;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn{
background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
display: block;
}
#menubar{
padding-top: 5px;
overflow: visible;
z-index: 10;
padding-left:0px;
padding-right: 0px;
margin: 0px;
}
#dropdownNavbar{
margin: 0px;
}

最佳答案

由于您使用的是 Bootstrap - 您还必须使用 jquery - 这是一种 jquery 方法。为您想要的事件颜色创建一个类,然后在单击时从所有按钮中删除该类,然后将其应用于被单击的按钮。

$(document).ready(function() {
$('.dropdown').click(function() {
$('.dropbtn').removeClass('dropbtn-active');
$(this).find('.dropbtn').addClass('dropbtn-active')
});

})
ul {
list-style-type: none;
padding-left: 1px;
}
.dropdown {
float: left;
padding-right: 1px;
}
.dropbtn {
background-color: #060975;
height: 30px;
border-radius: 0px 10px;
}
li button,
.dropbtn {
display: inline-block;
background-color: #3382d5;
color: white;
text-align: center;
}
.dropbtn-active {
background-color: #31b0d5;
}
li button:hover,
.dropdown:hover .dropbtn {
background-color: #31b0d5;
}
.dropdown:active .dropbtn {
background-color: #31b0d5;
}
button.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #e8f3f4;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.1);
z-index: 10;
}
.navbar-header {
overflow: visible;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 5px 10px;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn {
background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
display: block;
}
#menubar {
padding-top: 5px;
overflow: visible;
z-index: 10;
padding-left: 0px;
padding-right: 0px;
margin: 0px;
}
#dropdownNavbar {
margin: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div class="navbar-header">
<button style="background-color: #3382d5;" type="button" class="navbar-toggle" data-toggle="collapse" data-target="#menubar">
<span style="background-color: white;" class="icon-bar"></span>
<span style="background-color: white;" class="icon-bar"></span>
<span style="background-color: white;" class="icon-bar"></span>
</button>
</div>
<div id="menubar" class=" collapse navbar-collapse row">
<ul id="dropdownNavbar" class="nav navbar-nav">
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Time Card
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a id="showTimeCard" href="#">My Time Card</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> Project
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> HR Links
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-screenshot"></span> Leave Tracker
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-briefcase"></span> Accounts
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-headphones"></span> Service Desk
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-file"></span> Reports
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
<a href="#">Sub Menu 4</a>
<a href="#">Sub Menu 5</a>
</div>
</li>
<li class="dropdown">
<button type="button" class="btn dropbtn">
<span class="glyphicon glyphicon-cog"></span> Settings
<span class="caret"></span>
</button>
<div class="dropdown-content">
<a href="#">Sub Menu 1</a>
<a href="#">Sub Menu 2</a>
<a href="#">Sub Menu 3</a>
</div>
</li>
</ul>
</div>
</nav>

关于html - 如何在访问/激活子元素时更改父元素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com