Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how to use JavaScript dom to create and delete html element instances

伊谢尔伦
Release: 2017-07-20 13:16:47
Original
1541 people have browsed it

Using DOM, you can create new HTML elements and delete existing HTML elements.

(1) New element:


<script>
  //创建新的 <p> 元素
  var newEle=document.createElement("p");
  //创建文本节点
  var node=document.createTextNode("这是使用Javascript创建的新段落。");
  //将文本节点添加到新创建的 <p> 元素中
  newEle.appendChild(node);
  var p1_ele=document.getElementById("p1");
  //将新创建的元素添加到已有的元素中
  p1_ele.appendChild(newEle);
</script>
Copy after login

(2) Delete HTML element


##

<script>
   //删除一个元素时,必须首先获取到它的父元素
   function deleteEle(){
    var parent = document.getElementById("p_02");
    var child = document.getElementById("p_02_p2");
    parent.removeChild(child);
   }
   //使用代码获取元素的父元素
   function deleteEle3(){
    var child = document.getElementById("p_02_p3");
    child.parentNode.removeChild(child);
   }
</script>
Copy after login

Sample code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>Javascript 节点</title>
<head>
 <style>
  body {background-color:#e6e6e6}
 </style>
</head>
<body>
 <h3>(一)添加元素</h3>
 <p id="p1">
  <p id="p1">这是一个段落</p>
  <p id="p2">这是另一个段落</p>
 </p>
 <script>
  //创建新的 <p> 元素
  var newEle=document.createElement("p");
  //创建文本节点
  var node=document.createTextNode("这是使用Javascript创建的新段落。");
  //将文本节点添加到新创建的 <p> 元素中
  newEle.appendChild(node);
  var p1_ele=document.getElementById("p1");
  //将新创建的元素添加到已有的元素中
  p1_ele.appendChild(newEle);
 </script>
 <h3>(二)删除元素</h3>
 <p id="p_02">
  <p id="p_02_p1">这是一个段落</p>
  <p id="p_02_p2">这是另一个段落</p>
  <button onclick="deleteEle()">删除id为‘p_02_p2&#39;的元素</button><br/>
  <p id="p_02_p3">这是第三个段落</p>
  <button onclick="deleteEle3()">删除id为‘p_02_p3&#39;的元素(自动获取父元素)</button>
  <script>
   //删除一个元素时,必须首先获取到它的父元素
   function deleteEle(){
    var parent = document.getElementById("p_02");
    var child = document.getElementById("p_02_p2");
    parent.removeChild(child);
   }
   //使用代码获取元素的父元素
   function deleteEle3(){
    var child = document.getElementById("p_02_p3");
    child.parentNode.removeChild(child);
   }
  </script>
 </p>
</body>
</html>
Copy after login

The above is the detailed content of Detailed explanation of how to use JavaScript dom to create and delete html element instances. 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