HTML DOM match() 메서드
❮ 요소 개체예시
요소가 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";
}
정의 및 사용
이 matches()
메서드는 요소가 특정 CSS 선택기와 일치하는지 여부를 나타내는 부울 값을 반환합니다.
팁: 모든 CSS 선택기 목록은 CSS 선택기 참조 문서를 참조 하세요.
브라우저 지원
표의 숫자는 이 방법을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.
Method | |||||
---|---|---|---|---|---|
matches() | 33.0 | 18.0 | 34.0 | 7.0 | 21.0 |
통사론
element.matches(selectors)
매개변수 값
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. |
기술적 세부 사항
반환 값: |
CSS 선택기가 요소를 일치시킬 수 있는지 여부를 나타내는 부울:
|
---|
더 많은 예
예시
요소가 여러 선택기 중 하나와 일치하는지 확인합니다.
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.";
}
관련 페이지
JavaScript 참조: element.closest() 메서드.
❮ 요소 개체