This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Node.contains()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015년 7월⁩.

Node.contains() 메소드는 주어진 인자가 node 의 자손인지, 아닌지에 대한 Boolean 값을 리턴합니다.

Syntax

js
node.contains(otherNode);

Example

이 함수는 요소가 페이지의 body 안에 있는지 검사합니다. contains 는 포괄적이므로 node 가 body 자기 자신일 경우에도 true 가 반환됩니다. 만약 이걸 원하지 않는 경우에는 node 가 body 자기 자신인지 검사하여 false 를 반환하여 버리면 됩니다.

js
function isInPage(node) {
  return node === document.body ? false : document.body.contains(node);
}

명세서

Specification
DOM
# ref-for-dom-node-contains①

브라우저 호환성

See also