0

在以下情况下,当<span> class=add单击 时,如果它们出现在 parent 之后,我如何检查和删除任何<div>s与 class 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>
4

2 回答 2

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();
    });
});

演示:小提琴

于 2013-09-06T06:18:22.297 回答
1
$(".add").click(function() {
    $(this).parent().nextAll('.flag, .edit, .delete').remove();
});

小提琴

于 2013-09-06T06:20:21.477 回答