LoginSignup
6
8

More than 5 years have passed since last update.

BootStrapのドロップダウンメニューを閉じないようにする

Last updated at Posted at 2017-11-01

今更BootStrap感あるけど。

概要

https://getbootstrap.com/docs/3.3/components/#btn-dropdowns
このメニューの中チェックボックス入れた時に、ラベルクリックすると閉じちゃうのを止めたかった

こんなHTML

.btn-group
  %button.btn.btn-default.dropdown-toggle{ 'data-toggle': 'dropdown' }
    Action
    %span.caret
    %ul.dropdown-menu.dropdown-menu-right
      %li
        .checkbox
          %label
            %input{ type: :checkbox }
            Action

対応

BootStrapで、以下のようにページ全体でクリックイベント待ち受けしてメニューを閉じているので

$(document).on('click.bs.dropdown.data-api', clearMenus)

チェックボックスの親も含めて、クリックしてもイベントを伝播しないようにした。

$('.checkbox').on('click.bs.dropdown.data-api', (event) => event.stopPropagation())

メニュー閉じる動作は、ブラウザのネイティブなものではないので event. preventDefault() だと効かないのに注意。

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8