2015-09-10 86 views
0

我有一個模板:交換元素通過對

<div class="recursos_expandidos"> 
    <section>a</section> 
    <a href="#">b</a> 
    <section>c</section> 
    <a href="#">d</a> 
</div> 

而且我想反轉部分和主持人的位置,以便在div顯示:

<div class="recursos_expandidos"> 
    <a href="#">b</a> 
    <section>a</section> 
    <a href="#">d</a> 
    <section>c</section> 
</div> 

jQuery的或JavaScript將是巨大的,謝謝

回答

2

您可以使用疊代部分元素,他們的下一個兄弟後插入他們一個元素:

$('.recursos_expandidos section').each(function(){ 
    $(this).insertAfter($(this).next()); 
}); 

Working Demo

+0

它的工作原理,謝謝你,你救我-2H-3H! – Bernardao

+0

@Bernardao:很高興幫助:) –

0

可以使用after()有回調prev()next()after()

$('section').each(function() { 
 
    $(this).next('a').after(this) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="recursos_expandidos"> 
 
    <section>a</section> 
 
    <a href="#">b</a> 
 
    <section>c</section> 
 
    <a href="#">d</a> 
 
</div>

或者更簡單的方法幫助做到這一點

$('a').after(function() { 
 
return $(this).prev('section') 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="recursos_expandidos"> 
 
    <section>a</section> 
 
    <a href="#">b</a> 
 
    <section>c</section> 
 
    <a href="#">d</a> 
 
</div>