原生js怎么删除一个 div

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>增加删除DIV的JS写法</title>  
  6. </head>  
  7.   
  8. <body>  
  9. <div id="main">  
  10.     <div id="box">111</div>  
  11.     <div class="box2">222</div>  
  12. </div>  
  13. <script type="text/javascript">  
  14. var box = document.getElementById("box");  
  15. var main = document.getElementById("main");  
  16. var newMask = document.createElement("div");  
  17. newMask.id ="newMask";  
  18. main.appendChild(newMask);   
  19. if(box){  
  20.     box.parentNode.removeChild(box);  
  21. }  
  22. else{  
  23.     alert("没有这个div");  
  24. }  
  25. </script>  
  26. </body>  
  27. </html>  

 

 

[html] view plain copy
 
  1. <div id="main">  
  2.     <div id="box">111</div>  
  3.     <div class="box2">222</div>  
  4. </div>  
  5. <script type="text/javascript">  
  6. var box = document.getElementById("box");  
  7. box.parentNode.removeChild(box);  
  8. </script>  
posted @ 2017-12-12 16:13  baby孔祥超  阅读(21443)  评论(0编辑  收藏  举报