Home  >  Article  >  Web Front-end  >  What are the methods of document in js

What are the methods of document in js

下次还敢
下次还敢Original
2024-05-10 04:27:191356browse

JavaScript document object methods provide a series of methods for interacting with web documents, including: finding and returning elements (getElementById, getElementsByClassName, getElementsByTagName) creating elements and text nodes (createElement, createTextNode) manipulating the DOM structure (appendChild, insertBefore, removeChild, replaceChild) set and get style attributes (setAttribute, removeAttribute, getStyle

What are the methods of document in js

##JavaScript document object methods## The #JavaScript Document Object Model (DOM) provides a set of methods for interacting with web documents. These methods allow developers to manipulate document structure, style, and content.

Core Methods

    getElementById(id)
  • : Find and return the element by its id
  • getElementsByClassName(className)
  • : Find and return the element by its class name. Returns a set of elements.
  • getElementsByTagName(tagName)
  • : Finds and returns a set of elements by their tag name.
  • createElement(tagName)
  • : Create a new element.
  • createTextNode(text)
  • : Create a new text node.
  • appendChild(child)
  • : Append a child. Node is added to the end of the parent node.
  • insertBefore(newNode, referenceNode)
  • : Insert a new node before the specified reference node
  • removeChild(child).
  • : Remove a child node from the parent node.
  • replaceChild(newChild, oldChild)
  • : Replace an existing node with a new node. #Style methods

##setAttribute(attributeName, value): Set the specified attribute of the element

    removeAttribute(attributeName).
  • : Delete the specified attribute of the element.
  • getStyle(propertyName)
  • : Get the specified style attribute of the element.
  • setStyle(propertyName, value)
  • : Set the specified style attribute of the element.
  • Content methods

innerHTML: Gets or sets the HTML content of the element.

    innerText
  • : Gets or sets the text content of the element.
  • Event methods

addEventListener(eventName, eventHandler): Add an event handler to the element.

    removeEventListener(eventName, eventHandler)
  • : Remove an event handler from the element.
  • Other methods

cloneNode(deep): Clone an element, you can choose whether to clone the child node.

    write(text)
  • : Write text to the document.
  • close()
  • : Close the document stream.

The above is the detailed content of What are the methods of document in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use document in jsNext article:How to use document in js