Home > Web Front-end > JS Tutorial > body text

Methods of document objects in javascript

下次还敢
Release: 2024-05-08 22:21:22
Original
626 people have browsed it

The document object provides various methods to access and modify HTML documents, including: Get the element with the specified ID (via getElementById) Get the element collection based on the tag name (via getElementsByTagName) Get the element collection based on the class name (via getElementsByClassName ) Find elements using CSS selectors (via querySelector and querySelectorAll) Create new elements (via createElement) Remove elements from the document (via removeChild) Append elements to the document (via

Methods of document objects in javascript

Methods of the document object in JavaScript

The document object plays a vital role in JavaScript, providing a wide range of methods to access and modify HTML documents. The following are some common methods of document objects:

1. getElementById(id)

Get the HTML element with the specified ID.

Example:

<code class="javascript">const element = document.getElementById('my-element');</code>
Copy after login

2. getElementsByTagName(name)

Get the collection of HTML elements with the specified tag name

Example. :

<code class="javascript">const elements = document.getElementsByTagName('p');</code>
Copy after login

3. getElementsByClassName(name)

Get the collection of HTML elements with the specified class name

Example:

<code class="javascript">const elements = document.getElementsByClassName('my-class');</code>
Copy after login

4. querySelector(selector)

Use the CSS selector to find the first HTML element that meets the conditions. #Example:

<code class="javascript">const element = document.querySelector('div.my-div');</code>
Copy after login
5. querySelectorAll(selector)

Use CSS selector to find all HTML elements that meet the conditions

Example:

<code class="javascript">const elements = document.querySelectorAll('p.my-paragraph');</code>
Copy after login
6. createElement(name)

Create a new HTML element

Example:

<code class="javascript">const element = document.createElement('p');</code>
Copy after login
7. removeChild(element)

Remove an HTML element from the document

Example:

<code class="javascript">document.body.removeChild(element);</code>
Copy after login
8. appendChild(element)

Append an HTML element to the document.

Example:

<code class="javascript">document.body.appendChild(element);</code>
Copy after login
9. write(text)

Write text in the document.

Example:

<code class="javascript">document.write('Hello, world!');</code>
Copy after login
10. writeln(text)

Write text in the document and wrap it.

Example:

<code class="javascript">document.writeln('Hello, world!');</code>
Copy after login

The above is the detailed content of Methods of document objects in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template