Elenco a discesa Bootstrap JS


Classi CSS a discesa

Per un tutorial sui Dropdown, leggi il nostro Tutorial sui Dropdown 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

Via dati-* Attributi

Aggiungi data-toggle="dropdown"a un collegamento o un pulsante per attivare un menu a discesa.

Esempio

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

Tramite JavaScript

Abilita manualmente con:

Esempio

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

Nota: l'attributo data-toggle="dropdown" è obbligatorio indipendentemente dal fatto che tu chiami il metodo dropdown().


Opzioni a discesa

None

Metodi a discesa

La tabella seguente elenca tutti i metodi a discesa disponibili.

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

Eventi a discesa

La tabella seguente elenca tutti gli eventi a discesa disponibili.

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)

Suggerimento: usa event.relatedTarget di jQuery per ottenere l'elemento che ha attivato il menu a discesa:

Esempio

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