1. 程式人生 > >原生js怎麽刪除一個 div

原生js怎麽刪除一個 div

ech too tail set -m dtd 寫法 view data-

  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. <body>
  8. <div id="main">
  9. <div id="box">111</div>
  10. <div class="box2">222</div>
  11. </div>
  12. <script type="text/javascript">
  13. var box = document.getElementById("box");
  14. var main = document.getElementById("main");
  15. var newMask = document.createElement("div");
  16. newMask.id ="newMask";
  17. main.appendChild(newMask);
  18. if(box){
  19. box.parentNode.removeChild(box);
  20. }
  21. else{
  22. alert("沒有這個div");
  23. }
  24. </script>
  25. </body>
  26. </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>

原生js怎麽刪除一個 div