Bootstrap JS rozevírací seznam


Rozbalovací CSS třídy

Chcete-li získat výukový program o rozevíracích seznamech, přečtěte si náš výukový program pro rozevírací seznamy Bootstrap .

Class Description Example
.dropdown Indicates a dropdown menu
.dropdown-item Style links inside the dropdown menu with proper padding etc
.dropdown-item-text Style text or text links inside the dropdown menu with proper padding etc
.dropdown-menu Builds the dropdown menu
.dropdown-menu-right Right-aligns a dropdown menu
.dropdown-header Adds a header inside the dropdown menu
.dropup Indicates a dropup menu
.disabled Disables an item in the dropdown menu
.active Styles the active element in a dropdown menu
.divider Separates items inside the dropdown menu with a horizontal line

Přes data-* Atributy

Přidat data-toggle="dropdown"k odkazu nebo tlačítku pro přepínání rozbalovací nabídky.

Příklad

<button type="button" class="dropdown-toggle" data-toggle="dropdown">Dropdown Example</button>

Prostřednictvím JavaScriptu

Povolit ručně pomocí:

Příklad

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

Poznámka: Atribut data-toggle="dropdown" je povinný bez ohledu na to, zda voláte metodu dropdown().


Rozbalovací možnosti

None

Rozbalovací metody

V následující tabulce jsou uvedeny všechny dostupné rozevírací metody.

Method Description Try it
.dropdown("toggle") Toggles the dropdown. If set, it will open the dropdown menu by default
.dropdown("update") Updates the position of an element's dropdown
.dropdown("dispose") Destroys an element's dropdown

Rozbalovací události

V následující tabulce jsou uvedeny všechny dostupné rozevírací události.

Event Description Try it
show.bs.dropdown Occurs when the dropdown is about to be shown.
shown.bs.dropdown Occurs when the dropdown is fully shown (after CSS transitions have completed)
hide.bs.dropdown Occurs when the dropdown is about to be hidden
hidden.bs.dropdown Occurs when the dropdown is fully hidden (after CSS transitions have completed)

Tip: Použijte event.relatedTarget jQuery k získání prvku, který spustil rozbalovací nabídku:

Příklad

$(".dropdown").on("show.bs.dropdown", function(event){
  var x = $(event.relatedTarget).text(); // Get the text of the element
  alert(x);
});