programing tip

Twitter 부트 스트랩 드롭 다운을 숨기는 방법

itbloger 2020. 9. 11. 07:46
반응형

Twitter 부트 스트랩 드롭 다운을 숨기는 방법


이것은 성가신 일입니다. 부트 스트랩 드롭 다운에서 항목을 클릭해도 드롭 다운이 닫히지 않습니다. 드롭 다운 항목을 클릭 할 때 Facebox 라이트 박스를 열도록 설정했지만 문제가 있습니다.

여기에 이미지 설명 입력


내가 시도한 것

항목을 클릭하면 다음과 같이 시도했습니다.

    $('.dropdown.open').removeClass('open');
    $('.dropdown-menu').hide();

그것은 그것을 숨기지 만 어떤 이유로 다시 열리지 않을 것입니다.

보시다시피 드롭 다운이 열려있을 때 z-index엉망인 것처럼 보이기 때문에 닫아야합니다 (주로 드롭 다운이 Facebox 모달 상자 오버레이보다 높기 때문에).


Bootstrap의 내장 모달 상자를 사용하지 않는 이유

Bootstrap에 내장 된 멋진 모달 상자를 사용하지 않는 이유가 궁금하다면 다음과 같은 이유 때문입니다.

  1. AJAX를 사용하여 콘텐츠를로드하는 방법이 없습니다.
  2. 모달에 대해 매번 HTML을 입력해야합니다. Facebox로 간단하게 할 수 있습니다 :$.facebox({ajax:'/assets/ajax/dialogs/dialog?type=block-user&id=1234567'});
  3. 애니메이션을 적용하기 위해 CSS3 애니메이션을 사용하지만 (매우 멋져 보입니다) CSS3가 아닌 브라우저에서는보기 만하면별로 좋지 않습니다. Facebox는 JavaScript를 사용하여 페이드 인하므로 모든 브라우저에서 작동합니다.

이 시도:

$('.dropdown.open .dropdown-toggle').dropdown('toggle');

이것은 당신에게도 효과가 있습니다.

$('[data-toggle="dropdown"]').parent().removeClass('open');

여기에서 대부분의 옵션을 시도했지만 실제로는 나를 위해 일하지 않았습니다. ( $('.dropdown.open').removeClass('open');숨겨졌지만 다른 것을 클릭하기 전에 마우스를 가져 가면 다시 나타납니다.

그래서 나는 그것을 whith $("body").trigger("click")


$('.open').removeClass('open');

Bootstrap 4 (2018 년 10 월) :

$('.show').removeClass('show');

당신은 할 필요가 $('.dropdown.open').removeClass('open');드롭 다운 메뉴를 숨 깁니다 두 번째 줄을 제거합니다.


이는 다음 과 같이 앵커 태그에 data-toggle = "dropdown" 속성을 추가하여 JavaScript 코드를 작성하지 않고도 수행 할 수 있습니다 .

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="dropdown">
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
    Dropdown
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="dropdown">Action</a></li>
    <li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="dropdown">Another action</a></li>
  </ul>
</div>


이것은 나를 위해 일한 것입니다.

$(this).closest(".dropdown").find(".dropdown-toggle").dropdown("toggle");

The selected answer didn't work for me, but I'm thinking it's because of the newer version of Bootstrap. I ended up writing this:

$('.btn-navbar').addClass('collapsed');
$('.nav-collapse').removeClass('in').css('height', '0');

Hopefully that's helpful to someone else in the future.


Why use my method, because if the user gets out of the div, this metod allows the user a certain delay before the submenus dissapear. It's just like using the superfish plugin.

1) First download hover intent javascript

Link here : Hover intent javascript

2)Here is my code to execute

$(document).ready(function(){

        var config = {    
            over: showBootrapSubmenu,   
            timeout: 500,
            out: hideBootrapSubmenu  
        };

        function showBootrapSubmenu(){  
             $(this).addClass('open');
        }
        function hideBootrapSubmenu(){  
          $(this).removeClass('open');
            }

        //Hover intent for Submenus
        $(".dropdown").hoverIntent(config);

    });

Try this!

.removeClass("open").trigger("hide.bs.dropdown").trigger("hidden.bs.dropdown");

OR

$(clicked_element).trigger("click");

It always work for me.


Try to open HTML with Bootstrap Modal, I use this code:

$(function() {
     $('a[data-toggle=modal]').click(function(e) {
          e.preventDefault();
          var page = $(e.target).attr('href');
          var url = page.replace('#','');
          $(page).on('show', function () {
              $.ajax({
              url: "/path_to/"+url+".php/html/...",
              cache: false,
              success: function(obj){
                   $(page).css('z-index', '1999');
                   $(page).html(obj);
              }
              });
         })
     });
}); 

and the link is like this:

<a href="#my_page" data-toggle="modal">PAGE</a>

$('.dropdown.open').removeClass('open');

Reading the documentation and using JavaScript it could be done using the toggle method:

$('.button-class').on('click',function(e) {
    e.preventDefault();
    $('.dropdown-class').dropdown('toggle');
}

You can read about it in: http://getbootstrap.com/javascript/#dropdowns-methods


Here is my solution on how to close the button dropdown and toggle the button :

$(".btn-group.open", this)
    .trigger("click")
    .children("a.dropdown-toggle")
    .trigger("blur")

Where "this" is a global container of the button group.


this worked for me, i had a navbar and several pulldown menus.

$(document).ready(function () {
    $('a.dropdown-toggle').each(function(){
        $(this).on("click", function () {
            $('li.dropdown').each(function () {
                $(this).removeClass('open');
            });
        });
    });
});

The easiest way to do this is: you add a hide label:

<label class="hide_dropdown" display='hide'></label>

then everytime you want to hide dropdown, you call:

$(".hide_dropdown").trigger('click');

Don't know if this will help anyone (maybe it is too specific to my case, and not to this question) but for me this worked:

$('.input-group.open').removeClass('open');

After click:

// Hide mobile menu
$('.in,.open').removeClass('in open');
// Hide dropdown
$('.dropdown-menu').css('display','none');
setTimeout(function(){
    $('.dropdown-menu').css('display','');
},100);

Use class="dropdown-toggle" on the anchor tag, then when you click on the anchor tag it will close the bootstrap dropdown.


Hey this simple jQuery trick should help.

$(".dropdown li a").click(function(){
$(".dropdown").removeClass("open");
});

The easist way:

  $('.navbar-collapse a').click(function(){
    $('.navbar-toggle').trigger('click')
  })

you can use this code in your current event handler

$('.in,.open').removeClass('in open');

in my scenario I used when ajax call complete

jQuery(document).on('click', ".woocommerce-mini-cart__buttons .wc-forward.checkout", function() {
    var _this = jQuery(this);
    ajax_call(_this.attr("data-ajax-href"), "checkout-detail");
    $('.in,.open').removeClass('in open');
    return false;
});

Bootstrap 4.1:

$(".dropdown-link").closest(".dropdown-menu").removeClass('show');

참고URL : https://stackoverflow.com/questions/10941540/how-to-hide-twitter-bootstrap-dropdown

반응형