正如标题所示,我试图在单击下拉菜单并“打开”时将其添加到下拉菜单中,并在下拉菜单关闭后删除相同的类。我的代码在单击/打开下拉菜单时添加该类,但在下拉菜单关闭时不会将其删除。
到目前为止,这是我的代码:
<html>
<head>
</head>
<body>
<div class="dropdown" id="dropDownDiv">
<button class="btn btn-secondary dropdown-toggle dropdownButton" type="button"
id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
onclick="inputEffect()">
<span class="dropdownText" id="dropText">Vælg mærke</span><i
class="fas fa-chevron-down fasDropDown"></i>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
</body>
</html>
<script>
function inputEffect() {
var button = document.getElementById("dropdownMenuButton");
var element = document.getElementById("dropText");
if(button.hasAttribute("aria-expanded", "true")) {
element.classList.add("myEffect");
}
}
</script>
<style>
.myEffect {
position: absolute;
left: 11% !important;
top: -35% !important;
background-color: white;
color: #3faf8f;
}
</style>
弑天下
饮歌长啸
开满天机
狐的传说
相关分类