我想将一个元素移动到其兄弟元素内。
在下面的示例中,我希望将所有的.i-want-to-be-your-child
元素都移动到同一级别的兄弟元素.come-to-me
内。
原始HTML:
<div>
<a href="#" class="come-to-me">Welcome!</a>
<span class="i-want-to-be-your-child">Thank you!</span>
</div>
<div>
<a href="#" class="come-to-me">Welcome!</a>
<span class="i-want-to-be-your-child">Thank you!</span>
</div>
我希望最终结果是
<div>
<a href="#" class="come-to-me">Welcome!<span class="i-want-to-be-your-child">Thank you!</span></a>
</div>
<div>
<a href="#" class="come-to-me">Welcome!<span class="i-want-to-be-your-child">Thank you!</span></a>
</div>
jQuery
$(document).ready(function() {
$('.i-want-to-be-your-child').detach().appendTo($(sibling('.come-to-me')));
//$('.i-want-to-be-your-child').appendTo($(this).parent().child('.come-to-me'));
});
我做错了什么?实现这个的最佳方法是什么? 这里是jsfiddle链接