Trigger Bootstrap Dropdown with JS

后端 未结 2 1606
不知归路
不知归路 2021-02-07 05:25

I need to open a Bootstrap dropdown menu via JavaScript. This answer suggests adding and removing the open class, but when I try that, nothing happens. Adding a dif

相关标签:
2条回答
  • 2021-02-07 05:32

    Your main problem is that you aren't stopping the click event from propagating to the document. Bootstrap sets an event listener on the document that closes dropdowns.

    $('input').on('click', function (e) {
        e.stopPropagation();
        $(this).next('.dropdown').find('[data-toggle=dropdown]').dropdown('toggle');
    });
    

    jsFiddle - http://jsfiddle.net/8p6Wd/2/

    0 讨论(0)
  • 2021-02-07 05:37

    try this...

    <div class="dropdown">
        <input type="button" value="Classes" data-toggle="dropdown"/>
        <a data-toggle="dropdown" href="#">Dropdown Trigger</a>
        <div class="dropdown-menu">Dropdown Content</div>
    </div>
    
    0 讨论(0)
提交回复
热议问题