Metodo HTML DOM match()
❮ L'oggetto elementoEsempio
Determina se un elemento corrisponde a un selettore CSS:
var element = document.getElementById("myElement");
if (element.matches(".container")) {
element.innerHTML = "This element matches the \".container\" CSS selector";
} else {
element.innerHTML = "This element does not match the \".container\" CSS selector";
}
Definizione e utilizzo
Il matches()
metodo restituisce un valore booleano che indica se un elemento è abbinato o meno a uno specifico selettore CSS.
Suggerimento: per un elenco di tutti i selettori CSS, consulta il nostro Riferimento sui selettori CSS .
Supporto browser
I numeri nella tabella specificano la prima versione del browser che supporta completamente il metodo.
Method | |||||
---|---|---|---|---|---|
matches() | 33.0 | 18.0 | 34.0 | 7.0 | 21.0 |
Sintassi
element.matches(selectors)
Valori dei parametri
Parameter | Description |
---|---|
selectors |
Required. Specifies one or more CSS selectors to match the element. These are used to select HTML elements based on their id, classes, types, attributes, values of attributes, etc. For multiple selectors, separate each selector with a comma. The returned element depends on which element that is first found in the document (See "More Examples"). Tip: For a list of all CSS Selectors, look at our CSS Selectors Reference. |
Dettagli tecnici
Valore di ritorno: |
Un booleano, che indica se l'elemento può essere abbinato dal selettore CSS:
|
---|
Altri esempi
Esempio
Scopri se un elemento corrisponde a uno dei più selettori:
var element = document.getElementById("myElement");
if (element.matches(".container, .wrapper")) {
element.innerHTML = "This element matches either the \".container\" CSS selector or the \".wrapper\" selector.";
} else {
element.innerHTML = "This element does not match any of the selectors.";
}
Pagine correlate
Riferimento JavaScript: il metodo element.closest() .
❮ L'oggetto elemento