我在容器中有div.my-div
。如何将div
从容器移动到每个部分的部分?
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
答案 0 :(得分:2)
工作演示
$('div.my-div').each(function(){
$(this).closest('section').append($(this))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
答案 1 :(得分:0)
你可以这样做。
$('.my-div').each(function () {
$(this).closest('section').append(this.outerHTML);
$(this).remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
答案 2 :(得分:0)
试试这个:您可以找到每个my-div
的父部分,并使用.append()
将div移动到其父部分。
$(function(){
$('section div.container div.my-div').each(function(){
$(this).closest('section').append($(this));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>
<section>
<div class="container">container
<div class="my-div">my div</div>
</div>
</section>