HTML DOM Nearest() 메서드
❮ 요소 개체예시
CSS 선택기와 일치하는 가장 가까운 요소를 찾아 노란색 테두리를 지정합니다.
var element = document.getElementById("myElement");
var closest =
element.closest(".container");
if (closest) {
closest.style.border = "10px solid yellow";
}
정의 및 사용
메서드 는 closest()
지정된 CSS 선택기와 일치하는 가장 가까운 요소에 대해 DOM 트리를 검색합니다. 요소 자체에서 시작한 다음 일치하는 항목이 발견될 때까지 부모, 조부모 등을 테스트합니다. 일치하는 항목이 없으면 이 메서드는 null 을 반환합니다 .
브라우저 지원
표의 숫자는 이 방법을 완전히 지원하는 첫 번째 브라우저 버전을 지정합니다.
Method | |||||
---|---|---|---|---|---|
closest() | 41.0 | 15.0 | 35.0 | 6.0 | 28.0 |
통사론
element.closest(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. Tip: For a list of all CSS Selectors, look at our CSS Selectors Reference. |
기술적 세부 사항
반환 값: |
지정된 CSS 선택기와 일치하는 가장 가까운 조상 요소 또는 요소 자체입니다. 일치하는 항목이 없으면 null이 반환됩니다. 지정된 선택기가 유효하지 않으면 SYNTAX_ERR 예외를 던집니다. |
---|
관련 페이지
JavaScript 참조: element.matches() 메서드.
❮ 요소 개체