如果它们存在于父<div> </div> </div>之后,则搜索并删除特定类的<div>

时间:2013-09-06 06:12:33

标签: jquery

在下列情况下,点击<span> class=add后,如何查看并移除任何<div>s editflagdelete〜以及更多,如果它们出现在父<div>之后。

这些<div>如果存在则需要清除,因此可以在那里添加新的<div>。我熟悉remove(),但我很难找到多个<div>与上述类和父部分。

所有人都不可能在那里,我已经为这个例子硬编码了。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Side bar poll thingy</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $(".add").click(function() {
        // Find and remove all div's with classes flag, edit, delete, that may appear after the </div>
        // containing the <span> that was clicked 

    });
});
</script>
</head>
<body>

<div id="container">

    <div class="h1" data-id="1">Teachers <span class="add" data-id="US01">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
    <div class="h2" data-id="2">Who is your favorite Math teacher? <span class="add" data-id="US02">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
        <br>
    <div class="h1" data-id="8">Restaurants <span class="add" data-id="US10">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>
    <div class="h2" data-id="9">Which is your favourtie restaurant in town? <span class="add" data-id="US20">Add New</span></div>
    <div class="flag">Flag</div>
    <div class="edit">Edit</div>

</div>

</body>
</html>

2 个答案:

答案 0 :(得分:3)

尝试

$(document).ready(function() {
    $(".add").click(function() {
        // Find and remove all div's with classes flag, edit, delete, that may appear after the </div>
        // containing the <span> that was clicked 

        var $this = $(this);
        $this.parent().nextUntil('div:has(.add)', '.flag, .edit, .delete').remove();
        //$this.parent().nextUntil('div:h1', '.flag, .edit, .delete').remove();
    });
});

演示:Fiddle

答案 1 :(得分:1)

$(".add").click(function() {
    $(this).parent().nextAll('.flag, .edit, .delete').remove();
});

Fiddle