简体   繁体   English

从 javascript 调用的 Bootstrap 5 beta 下拉问题

[英]Bootstrap 5 beta dropdown issue calling from javascript

I have a dropdown that was working in bootstrap 4, but now in bootstrap 5 (beta) it is no longer working.我有一个在引导程序 4 中工作的下拉菜单,但现在在引导程序 5(测试版)中它不再工作。 What I am trying to do is open a specific dropdown from a secondary button but this no longer works any more due to the changes they made in bootstrap 5. There is a fiddle to demonstrate.我想要做的是从辅助按钮打开一个特定的下拉菜单,但由于他们在引导程序 5 中所做的更改,这不再有效。有一个小提琴要演示。 When you click the main button it works fine but when you click the second button it has an error, this method of just calling the dropdown show method from anywhere worked perfectly in bootstrap 4. How can I get it to work in bootstrap 5, there are mentions of a 'reference' option in the docs but all the examples in the docs are only for data-attributes, not for JS.当您单击主按钮时它工作正常,但是当您单击第二个按钮时它有一个错误,这种从任何地方调用下拉显示方法的方法在引导程序 4 中完美运行。我怎样才能让它在引导程序 5 中工作,那里在文档中提到了“参考”选项,但文档中的所有示例仅适用于数据属性,不适用于 JS。 I have tried a bunch of stuff but nothing works.我尝试了很多东西,但没有任何效果。 Thanks.谢谢。

https://jsfiddle.net/6hbcp9xm/ https://jsfiddle.net/6hbcp9xm/

HTML: HTML:

  <div class="col">
    <div id="test" class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
        Dropdown button
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
        <li><a class="dropdown-item" href="#">Action</a></li>
        <li><a class="dropdown-item" href="#">Another action</a></li>
        <li><a class="dropdown-item" href="#">Something else here</a></li>
      </ul>
    </div>
  </div>
  <div class="col">
    <button id="testbtn">
      test
    </button>
  </div>
</div>

JS: JS:

$("#testbtn").on("click", function(){
    $("#test").dropdown("show");
})

The id you tried to show was wrapped around the first dropdown button.您尝试显示的 id 包裹在第一个下拉按钮周围。 This makes it try to show the button, which is already shown so the script runs into an error.这使它尝试显示按钮,该按钮已显示,因此脚本运行出错。

Use toggleClass in combination with the class of the dropdown which is .dropdown-menu .toggleClass.dropdown-menu下拉菜单的 class 结合使用。

$("#testbtn").on("click", function(){
    $('.dropdown-menu').toggleClass('show')
})

https://jsfiddle.net/nzx1c04y https://jsfiddle.net/nzx1c04y

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

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