简体   繁体   中英

close bootstrap drop down Jquery

i have a bootstrap dropdwon , i need it only open and close when press in 'li.dropdown a' and close when i press on the body .. i make it when open and close from only click in the icon . but need the code that make me close it when i click in the body .

open and close with icon ( Working ) :

$('li.dropdown a').on('click', function (event) {
               $('li.dropdown.setting-widget').toggleClass("open");
    });

**** close when i click in body (not Working ) :****

    if (!$('li.dropdown.setting-widget').hasClass("open")) {
     $('body').on('click', function (event) {
               $('li.dropdown.setting-widget').toggleClass("open");
    });
    }   

Assuming all the context is correct, this code should work.

Your if statement needs to be inside the method.

$('body').on('click', function (event) {
  if ($('li.dropdown.setting-widget').hasClass("open")) {
      $('li.dropdown.setting-widget').toggleClass("open");
  }
});

If it does not work, you should add more context like some example HTML, or a JS fiddle showing the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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