Home > Web Front-end > JS Tutorial > How to delete dom in javascript

How to delete dom in javascript

藏色散人
Release: 2023-01-03 09:31:47
Original
7992 people have browsed it

How to delete dom in javascript: First create an HTML sample file and create a button button; then delete the node by using the "removeChild()" method.

How to delete dom in javascript

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

JS Implementation of DOM Delete Node Operation Example

1. Introduction

Deleting nodes is achieved by using the removeChild() method.

removeChild() method is used to delete a child node.

obj. removeChild(oldChild)

oldChild: Indicates the node that needs to be deleted.

2. Use

to delete nodes. This example will dynamically delete the selected text on the page through the removeChild() method of the DOM object.

3. Code

<!DOCTYPE html>
<html>
<head>
<title>删除节点</title>
<script language="javascript">
<!--
function delNode()
{
var deleteN=document.getElementById(&#39;di&#39;);
if(deleteN.hasChildNodes())
{
  deleteN.removeChild(deleteN.lastChild);
}
}
-->
</script>
</head>
<body>
<h1>删除节点</h1>
<div id="di">
<p>第一行文本</p>
<p>第二行文本</p>
<p>第三行文本</p>
</div>
<form>
<input type="button" value="删除" onclick="delNode();"/>
</form>
</body>
</html>
Copy after login

[Recommended learning: js basic tutorial]

The above is the detailed content of How to delete dom 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