gpt4 book ai didi

javascript - 如何将悬停规则保持在 Bootstrap 下拉按钮之上

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:45 24 4
gpt4 key购买 nike

我正在处理以下代码段。如何让悬停规则在 .btn-default 上受到影响,同时悬停在其关联的 .dropdown-menu 上?

$(function(){
$('.btn-dropdown').hover(function() {
$(this).addClass('open');
},
function() {
$(this).removeClass('open');
});
});
body{
padding:30px;
}
.btn-default:hover{
color:red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="col-md-3">
<div class="btn-group btn-group-justified" role="group" aria-label="...">
<div class="btn-group btn-dropdown" role="group">
<a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
List One
<i class="fa fa-chevron-right" aria-hidden="true"></i>
</a>
<ul class="dropdown-menu">
<li><a href="#">Dropdown link</a></li>
<li><a href="#">Dropdown link</a></li>
</ul>
</div>

<div class="btn-group btn-dropdown" role="group">
<a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
List Two
<i class="fa fa-chevron-right" aria-hidden="true"></i>
</a>
<ul class="dropdown-menu">
<li><a href="#">Dropdown link</a></li>
<li><a href="#">Dropdown link</a></li>
</ul>
</div>
</div>
</div>

最佳答案

看看这是否正是您要找的。我在您的 CSS 中添加了 .open parent 并删除了 .hover

每当你附加 .open 类时,它都会添加 CSS

$(function(){
$('.btn-dropdown').hover(function() {
$(this).addClass('open');
},
function() {
$(this).removeClass('open');
});
});
body{
padding:30px;
}
.open .btn-default{
color:red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="col-md-3">
<div class="btn-group btn-group-justified" role="group" aria-label="...">
<div class="btn-group btn-dropdown" role="group">
<a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
List One
<i class="fa fa-chevron-right" aria-hidden="true"></i>
</a>
<ul class="dropdown-menu">
<li><a href="#">Dropdown link</a></li>
<li><a href="#">Dropdown link</a></li>
</ul>
</div>

<div class="btn-group btn-dropdown" role="group">
<a role="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
List Two
<i class="fa fa-chevron-right" aria-hidden="true"></i>
</a>
<ul class="dropdown-menu">
<li><a href="#">Dropdown link</a></li>
<li><a href="#">Dropdown link</a></li>
</ul>
</div>
</div>
</div>

关于javascript - 如何将悬停规则保持在 Bootstrap 下拉按钮之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658679/

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