Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add, edit and delete operations in JavaScript

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report