Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. Node
  4. contains()

Node: contains() method

Baseline Widely available

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

Thecontains() method of theNode interfacereturns a boolean value indicatingwhether a node is a descendant of a given node, that is the node itself,one of its direct children (childNodes),one of the children's direct children, and so on.

Note:A node iscontained inside itself.

Syntax

js
contains(otherNode)

Parameters

otherNode

TheNode to test with.

Note:otherNode is not optional, but can be set tonull.

Return value

A boolean value that istrue ifotherNode is contained in the node,false if not.

If theotherNode parameter isnull,contains() always returnsfalse.

Example

This function checks to see if an element is in the page's body. Ascontains is inclusive and determining if the body contains itself isn't theintention ofisInPage this case explicitly returnsfalse.

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

Specifications

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp