【问题标题】:Remove second div with same class删除具有相同类的第二个 div
【发布时间】:2015-02-05 13:58:42
【问题描述】:

在我的 HTML 中有两个具有相同类的 div。如果我想按类删除 div,它们都会被删除。我如何(在使用 Jquery 时)按类删除第二个 div?有没有办法让它“跳过”它找到的第一个 div?

代码如下所示:

<div class="wrapper">
  <div class="randomdiv">
    <div class="oneofthetwodivs">
    </div>
  </div>
  <div class="randomdiv"></div>
  <div class="randomdiv"></div>
  <div class="oneofthetwodivs">
</div>

【问题讨论】:

    标签: jquery class


    【解决方案1】:

    您可以使用eq() 来定位具有该类的第二个元素(它从零开始):

    $('.oneofthetwodivs').eq(1).remove();
    

    【讨论】:

    • 嗯,这是一些快速的服务!谢谢分配!
    【解决方案2】:

    这应该可行:

    $(".oneofthetwodivs").eq(1).remove();
    

    $(".oneofthetwodivs:last").remove();
    

    【讨论】:

      【解决方案3】:

      你想在你的课堂上使用:nth-of-type(2)

      $(document).ready(function() {
          $('.removeMe:nth-of-type(2)').hide();
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="removeMe">This is the first div</div>
      <div class="removeMe">This is the SECOND div, should not show</div>
      <div class="removeMe">This is the third div</div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-02
        • 1970-01-01
        • 1970-01-01
        • 2017-02-05
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2019-11-26
        相关资源
        最近更新 更多