Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

jquery - How to hide Twitter Bootstrap dropdown

This is getting annoying — when I click on an item in a Bootstrap dropdown, the dropdown doesn't close. I have it set up to open a Facebox lightbox when you click the dropdown item but there is a problem with it.

enter image description here


What I have tried

When the item is clicked, I tried doing this:

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

That hides it, but then for some reason it won't open again.

As you can see I really need the dropdown to close, because it looks crappy when it stays open (mainly because the z-index of the dropdown is higher than the Facebox modal box overlay.


Why I'm not using Bootstrap's built-in modal box

If you're wondering why I'm not using the nice-looking modal box built into Bootstrap, it is because:

  1. It doesn't have a way to load content into it with AJAX.
  2. You have to type HTML each time for the modal; with Facebox you can do a simple: $.facebox({ajax:'/assets/ajax/dialogs/dialog?type=block-user&id=1234567'});
  3. It uses CSS3 animations to animate (which looks very nice) but in non-CSS3 browsers it just shows, which doesn't look that nice; Facebox uses JavaScript to fade in so it works in all browsers.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

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

This may also work for you:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...