当点击<div>中的<span>时,jQuery在<div> </div>之后添加内容</div> </span>

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

标签: jquery

在以下代码中,当我点击<div></div>内的<span>时,我试图在包含<span>的{​​{1}}之后添加内容。

这是地方:

<div></div>

因此,如果我点击<div id="container"> <div class="h1" data-id="1">Teachers <span class="add" data-id="US01">Add New</span></div> <div class="h2" data-id="2">Who is your favorite Math teacher? <span class="add" data-id="US02">Add New</span></div> <br> <div class="h1" data-id="8">Restaurants <span class="add" data-id="US10">Add New</span></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> <span>,则需要在包含add的{​​{1}}之后添加内容。问题是,现在它在<div>之后添加了内容。如何在包含<span>

</span>后添加

以下是我正在尝试的其余代码:

</div>

1 个答案:

答案 0 :(得分:2)

使用.parent()方法:

$(this).parent("div").after("<div class='newbox'>I'm new box by append</div>");

DEMO FIDDLE