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

Detailed explanation of how JavaScript can quickly and dynamically delete and delete dom element code

伊谢尔伦
Release: 2017-07-21 09:50:13
Original
2913 people have browsed it

In DOM we can easily and quickly dynamically delete and delete dom elements

Example 1:

Dynamicly create a button

<html> 
<head> 
<title>动态创建按钮</title> 
<script language="javascript"> 
var a,b,ab,ba,c;
function createInputA(){
a = document.createElement("input");  //使用DOM的创建元素方法
a.type = "button" ;                       //设置元素的类型
a.value = "按钮A";                     //设置元素的值
a.attachEvent("onclick",addInputB);    //为控件添加事件
document.body.appendChild(a);             //添加控件到窗体中
//a = null;              //释放对象
}
Copy after login

Example 2:

<html>
<head>
<script type="text/javascript">
    function test(){        
        //createElement()  创建一个指定标签名的元素[比如:动态创建超链接]
        var createa=document.createElement("a");
        createa.id="a1";
        createa.innerText="连接到百度";
        createa.href="http://www.jb51.net";
        //createa.color="green" ////添加颜色(不要忘记style属性,不然没有效果)
        createa.style.color="green"
        //添加默认位置--body 并且添加子节点
        //document.body.appendChild(createa);
        //放置指定位置
        document.getElementById("p1").appendChild(createa);
    }
    
    function test2(){
        //指定位置删除节点removeChild()
        document.getElementById("p1").removeChild(document.getElementById("a1")); //id 名重复 js只取第一个
    }
</script>
</head>
<body>
<!--动态创建元素-->
<input type="button" value="创建一个a标签" onclick="test()"/><br/>
<input type="button" value="删除创建一个a标签" onclick="test2()"/>
<p id="p1" style="width:400px;height:300px;border:1px solid silver">
</p>
</body>
</html>
Copy after login

Dynamicly create multiple forms:

<html>
 <head>
  <script type="text/javascript">
   window.onload = function() {
    var aBtn = document.createElement("input");
    var bBtn = document.createElement("input");
    var cBtn = document.createElement("input");
    
    aBtn.type = "button";
    aBtn.value = "按钮A";
    aBtn.onclick = copyBtn;
    
    bBtn.type = "button";
    bBtn.value = "按钮B";
    bBtn.onclick = copyBtn;
    
    cBtn.type = "button";
    cBtn.value = "按钮C";
    cBtn.onclick = clearCopyBtn;
    
    document.body.appendChild(aBtn);
    document.body.appendChild(bBtn);
    document.body.appendChild(cBtn);
   };
   
   function copyBtn() {
    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = this.value;
    btn.isCopy = true;
    btn.onclick = copyBtn;
    document.body.appendChild(btn);
   }
   
   function clearCopyBtn() {
    var btns = document.getElementsByTagName("input");
    var length = btns.length;
    for (var i = length - 1; i >= 0; i--) {
     if (btns[i].isCopy) {
      document.body.removeChild(btns[i]);
     }
    }
   }
  </script>
 </head>
 <body>
  
 </body>
</html>
Copy after login


The above is the detailed content of Detailed explanation of how JavaScript can quickly and dynamically delete and delete dom element code. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!