首页 > Web开发 > 详细

jQuery移除指定元素后的所有元素

时间:2016-04-18 11:38:09      阅读:271      评论:0      收藏:0      [点我收藏+]

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移除指定元素后的所有元素

原文:http://www.cnblogs.com/hello-word1/p/5403485.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!