HTML DOM match() Method
❮ Objekt prvkuPříklad
Zjistěte, zda prvek odpovídá selektoru 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";
}
Definice a použití
Metoda matches()
vrací booleovskou hodnotu, která označuje, zda prvek odpovídá konkrétnímu selektoru CSS či nikoli.
Tip: Seznam všech selektorů CSS najdete v naší Referenční příručce selektorů CSS .
Podpora prohlížeče
Čísla v tabulce určují první verzi prohlížeče, která tuto metodu plně podporuje.
Method | |||||
---|---|---|---|---|---|
matches() | 33.0 | 18.0 | 34.0 | 7.0 | 21.0 |
Syntax
element.matches(selectors)
Hodnoty parametrů
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. |
Technické údaje
Návratová hodnota: |
Boolean označující, zda lze prvek porovnat pomocí selektoru CSS:
|
---|
Další příklady
Příklad
Zjistěte, zda prvek odpovídá jednomu z více selektorů:
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.";
}
Související stránky
Reference JavaScript: Metoda element.closest() .
❮ Objekt prvku