jQuery移除指定元素后的全部元素

jQuery 遍历的nextAll() 方法能够搜索 DOM 树中的元素跟随的同胞元素,也就是一个元素后面的全部同级元素,删除能够使用方法remove(),因此连起来为css

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

下面给出实例演示:点击按钮后,删除被选项目以后的全部选项jquery

  1. 建立Html元素spa

    <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样式3d

    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代码blog

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

  • 选择项目rem

  • 点击删除按钮,被选项以后的全部同级元素都被删除get

相关文章
相关标签/搜索