Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Javascript removes the HTML node operation


May 30, 2021 Article blog



Removing nodes on an HTML page is similar to adding a paragraph to a specified attachment point in adding a paragraph to a specified attachment point with the code .body.insertBefore (newGraf, docForm) and deleting it as document.body.removeChild (deleteNode). The removeChild method is used to remove the elements inside the body field.

Take a look at the code.

Suppose you have the following piece of code with four paragraphs.

<html>
<head>
<title>Deleting Nodes</title>
</head>
<body>
<p>给你一张过去的CD,听听那时我们的爱情,有时会突然忘了,我还在爱着你</p>
<p>再唱不出那样的歌曲,听到都会红着脸躲避,虽然会常常忘了,我仍然爱着你</p>
<p>由于爱情,不会轻易悲伤,所以一切都是幸福的样子</p>
<p>由于爱情,简单的生长,仍然随时都能为你疯狂</p>
<input type="button" onclick="deleteNodes()" value="delete Nodes"/>
<script> function deleteNodes() { } </script>
</body>
</html>

Now that you want to delete the paragraph content, how do I implement the deleteNodes method?

Quite simply, the two lines of code are fixed.

var deleteNode =document.getElementsByTagName("p")[0];
document.body.removeChild(deleteNode);

var deleteNode =document.getElementsByTagName("p")[0];

To get to the paragraph to be deleted, document.getElementsByTagName ("p") is the first p-tag content in the body label domain.

document.body.removeChild(deleteNode);

The node information you get to be deleted is removed from the top of the page, using the removeChild method.

If the paragraph has an id, you can use the getElementById method directly to specify the object to be deleted, instead of using the getElementsByTagName method to get to the object collection, and then specify the deleted object by subscripting the array.

Our example removes document.getElementsByTagName ("p") and also deletes other paragraphs, such as .getElementsByTagName ("p").