HTML DOM match() Metoda
❮ Obiekt elementuPrzykład
Określ, czy element pasuje do selektora 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";
}
Definicja i użycie
Metoda matches()
zwraca wartość logiczną wskazującą, czy element jest dopasowany przez określony selektor CSS, czy nie.
Porada: Listę wszystkich selektorów CSS znajdziesz w naszej dokumentacji selektorów CSS .
Obsługa przeglądarki
Liczby w tabeli określają pierwszą wersję przeglądarki, która w pełni obsługuje tę metodę.
Method | |||||
---|---|---|---|---|---|
matches() | 33.0 | 18.0 | 34.0 | 7.0 | 21.0 |
Składnia
element.matches(selectors)
Wartości parametrów
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. |
Szczegóły techniczne
Wartość zwrotu: |
Wartość logiczna, wskazująca, czy element może być dopasowany przez selektor CSS:
|
---|
Więcej przykładów
Przykład
Dowiedz się, czy element pasuje do jednego z wielu selektorów:
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.";
}
Powiązane strony
Dokumentacja JavaScript: metoda element.closest() .
❮ Obiekt elementu