Home > Web Front-end > JS Tutorial > How to add child nodes in JavaScript

How to add child nodes in JavaScript

青灯夜游
Release: 2023-01-07 11:47:22
Original
7500 people have browsed it

How to add child nodes in JavaScript: first use the "document.getElementById("id")" statement to get the parent element node according to the specified id value; then use "parent node.innerHTML="name>"" statement adds a child node.

How to add child nodes in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript to add child nodes

Create an HTML file like this:

How to add child nodes in JavaScript

In In the script tag, write the js code:

Get the div element:

How to add child nodes in JavaScript

After obtaining it, you can operate it , use innerHTML to add the sub-element p inside it:

How to add child nodes in JavaScript

In this way, we have added a p element to this div, if other elements need to be added inside If so, you can also write directly:

How to add child nodes in JavaScript

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to add child nodes in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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