zoukankan      html  css  js  c++  java
  • 移除(删除)指定元素后的所有元素 jquery

     

     

    jQuery 遍历的nextAll() 方法可以搜索 DOM 树中的元素跟随的同胞元素,也就是一个元素后面的所有同级元素,删除可以使用方法remove(),所以连起来为

    $(selector).nextAll(“条件”).remove();

    下面给出实例演示:点击按钮后,删除被选项目之后的所有选项

    1. 创建Html元素

      <div class="box">
      <span>点击按钮后,删除被选项目之后的所有选项。</span><br>
      <div class="content">
      <input type="checkbox" name="item"><span>萝卜</span>
      <input type="checkbox" name="item"><span>青菜</span>
      <input type="checkbox" name="item"><span>小葱</span><br>
      <input type="checkbox" name="item"><span>豆腐</span>
      <input type="checkbox" name="item"><span>土豆</span>
      <input type="checkbox" name="item"><span>茄子</span><br>
      </div>
      <input type="button" value="删除">
      </div>
    2. 简单设置一下css样式

      div.box{300px;height:200px;padding:10px 20px;border:4px dashed #ccc;}
      div.box>span{color:#999;font-style:italic;}
      div.content{250px;height:100px;margin:10px 0;border:1px solid green;}
      input{margin:10px;}
      input[type='button']{200px;height:35px;margin:10px;border:2px solid #ebbcbe;}
    3. 编写jquery代码

      $(function(){
      $("input:button").click(function() {
      $("input:checkbox:checked").next().nextAll().remove();
      });
      })
    4. 观察显示效果

    • 选择项目

    • 点击删除按钮,被选项之后的所有同级元素都被删除

  • 相关阅读:
    Python日期和时间
    Python实现ATM
    XML的ElementTree解析方式
    Python多线程
    Python文件操作
    Python错误和异常
    Python基础第四课
    html页面引入另一个html页面
    微信直播video安卓端始终在最顶层的解决方法
    设计模式之 外观模式
  • 原文地址:https://www.cnblogs.com/wangking/p/9291398.html
Copyright © 2011-2022 走看看