Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to add, edit and delete operations in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 12:47:47 10月04日 Update

This article mainly introduces how to add, edit, delete JavaScript operation related knowledge, the content is easy to understand, the operation is simple and fast, with a certain reference value, I believe you read this JavaScript how to add, edit, delete the operation of the article will have a harvest, let's take a look.

The details are as follows:

CreateElement,createTextNode,appendChild,insertBefore,removeChild,replaceChild

CreateElement

The following code is used to create the

Elements:

Var para = document.createElement ("p"); createTextNode

For

Element to add a text node:

Var node = document.createTextNode ("this is a new paragraph.") ; appendChild (it is used to add new elements to the tail)

Add a text node to the

In the element:

Para.appendChild (node)

The above three combined cases:

This is a paragraph.

This is another paragraph.

Var para = document.createElement ("p"); / / create the p tag var node = document.createTextNode ("this is a new paragraph.") ; / add a text node para.appendChild (node); / / add a text node (content) var element = document.getElementById ("div1") to the p tag; element.appendChild (para); / / add to an existing element

Running result:

InsertBefore (new element added to the starting position) this is a paragraph.

This is another paragraph.

Var para = document.createElement ("p"); var node = document.createTextNode ("this is a new paragraph.") ; para.appendChild (node); var element = document.getElementById ("div1"); var child = document.getElementById ("p1"); element.insertBefore (para, child)

Running result:

RemoveChild

To remove an element, you need to know the parent of the element.

This is a paragraph.

This is another paragraph.

Var parent = document.getElementById ("div1"); var child = document.getElementById ("p1"); parent.removeChild (child)

Running result:

ReplaceChild

Use the replaceChild () method to replace elements in HTML DOM.

This is a paragraph.

This is another paragraph.

Var para = document.createElement ("p"); var node = document.createTextNode ("this is a new paragraph.") ; para.appendChild (node); var parent = document.getElementById ("div1"); var child = document.getElementById ("p1"); parent.replaceChild (para, child)

Running result:

This is the end of the article on "how to add, edit and delete JavaScript". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to add, edit and delete JavaScript". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Paragraph this is element text result node run content knowledge tag article code value location tail easy to operate article method easy to understand more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Apple MySQL macOS