我有以下代码,我想移动' a'标记在两个父母之上(other-div和some-other-div)
<div class="container">
<div class="some-other-div">
<div class="other-div">
<a class="link" href="#">move me<a>
</div>
</div>
<div class="some-other-div">
<div class="other-div">
<a class="link" href="#">move me<a>
</div>
</div>
</div>
看起来像这样
<div class="container">
<a class="link" href="#">move me<a>
<div class="some-other-div">
<div class="other-div">
</div>
</div>
<a class="link" href="#">move me<a>
<div class="some-other-div">
<div class="other-div">
</div>
</div>
</div>
这是我创建的一支笔:
http://codepen.io/florinsimion/pen/qqRWOo
任何想法?
答案 0 :(得分:2)
使用before()
方法和回调函数迭代元素并在回调内部返回要移动的a
标记。
$('.some-other-div').before(function() {
return $(this).find('.link');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="some-other-div">
<div class="other-div">
<a class="link" href="#">move me</a>
</div>
</div>
<div class="some-other-div">
<div class="other-div">
<a class="link" href="#">move me</a>
</div>
</div>
</div>