首页 > jQuery 如何 删除一个div,不是全部

jQuery 如何 删除一个div,不是全部

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="http://cdn.uedsc.com/jquery/1.8.3/jquery.min.js"></script>
   <style>
   p {
     float: left;
   }
   .ad {
    color: red;
   }
   </style>
</head>
<body>
  
  <button class="a1">点击</button>
  <button class="a2">删除</button>
  <div class="boxes">
      <p class="text"></p>
  </div>
  <script>
  $(".a1").click(function(){
    var showimg = "<div class='ad'>123456</div>";
    $(".text").append(showimg);
  });

  $(".a2").click(function() {
    $("div.ad").remove();
  });

  </script>
</body>
</html>

点击删除的时候,我想一个一个删除,不是一下全给删除了。
怎么做到添加N个,点击删除时,只一个一个删除刚刚添加的?


a2的JS部分改成:

$(".a2").click(function() {
    $('.text div:last-child').remove();
  });

jQuery的remove方法,删除指定元素,如果要删除刚刚添加的元素,要写代码完成了


通过eq()方法指定某个删除

【热门文章】
【热门文章】