简体   繁体   English

引导下拉菜单按钮在单击按钮以及单击屏幕时更改

[英]bootstrap dropdown menu button change when clicked on button and also when clicked on the screen

I have this bootstrap dropdown here .我在这里有这个引导下拉菜单。 What I want to do is to change the background color of the button to blue only when the dropdown menu is expanded.我想要做的是仅在展开下拉菜单时将按钮的背景颜色更改为蓝色。 I achieve it by styling it through adding a click listener to the button but the issue is when the menu is expanded and the user clicks somewhere else, the menu goes hidden while the button is still blue.我通过向按钮添加点击侦听器来设置它的样式来实现它,但问题是当菜单展开并且用户点击其他地方时,菜单在按钮仍然为蓝色时隐藏。 How can I modify it so the button is blue ONLY AND ONLY when the menu is expanded?如何修改它,使按钮在菜单展开时为蓝色?

here is my code:这是我的代码:

//template
<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" :style="test ? clickedBtn : null" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @click="testing()">
        Dropdown button
    </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>

//script
  data: {
    test: false,
    clickedBtn: {
      background: "blue",
      color: "white"  
    }
  },
  methods: {
    testing(){
        this.test = !this.test
    }
  }

From your original description, it seemed like you wanted the button's background color to switch to blue whenever the button was activated and to change back to the normal color when either the button is reclicked or the user clicks somewhere else on the page.从您的原始描述来看,您似乎希望按钮的背景颜色在按钮被激活时切换为蓝色,并在重新单击按钮或用户单击页面上的其他位置时更改回正常颜色。 This code will do that:此代码将执行此操作:

 #dropdownMenuButton[aria-expanded="true"] { background-color: blue; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script> <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle":style="test? clickedBtn: null" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" @click="testing()"> Dropdown button </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>

If you want to change the background color for the dropdown class (which is just the area behind the button – the dropdown itself is positioned absolutely and is separate), then you could use a MutationObserver on the switch and then change the background color for the parent dropdown div.如果您想更改下拉菜单 class 的背景颜色(这只是按钮后面的区域 - 下拉菜单本身是绝对定位的并且是独立的),那么您可以在开关上使用MutationObserver然后更改背景颜色父下拉 div。

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

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