移除(删除)指定元素后的所有元素 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{width:300px;height:200px;padding:10px 20px;border:4px dashed #ccc;}
    div.box>span{color:#999;font-style:italic;}
    div.content{width:250px;height:100px;margin:10px 0;border:1px solid green;}
    input{margin:10px;}
    input[type='button']{width:200px;height:35px;margin:10px;border:2px solid #ebbcbe;}
  3. 编写jquery代码

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

  • 选择项目

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

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

扫描二维码关注公众号,回复: 2058505 查看本文章
$(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{width:300px;height:200px;padding:10px 20px;border:4px dashed #ccc;}
    div.box>span{color:#999;font-style:italic;}
    div.content{width:250px;height:100px;margin:10px 0;border:1px solid green;}
    input{margin:10px;}
    input[type='button']{width:200px;height:35px;margin:10px;border:2px solid #ebbcbe;}
  3. 编写jquery代码

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

  • 选择项目

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

猜你喜欢

转载自www.cnblogs.com/wangking/p/9291398.html