2016-05-26 5 views
1

Ich habe zwei identische <ul> Liste, muss ich die Reihenfolge der beiden Liste mit jQuery, wenn ich einen Knopf drücken.Ich habe versucht, den Balg-Code und es ist nicht Arbeiten wie erwartet.Umgekehrte Reihenfolge von zwei Liste zur gleichen Zeit mit der gleichen Reihenfolge

function spin(){ 
 
    spinList($('.list-view')); 
 
} 
 

 
var spinList  = function($el){ 
 
     
 
     var elementClone = $el.eq(0).clone(); 
 
     var allLength = elementClone.find('li').length; 
 
     for(var i = 0;i<allLength;i++){ 
 
      var element = elementClone.find('li').eq(allLength-i).clone(); 
 
      $el.eq(0).find('li').eq(i).replaceWith(element); 
 
      $el.eq(1).find('li').eq(i).replaceWith(element); 
 
     } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button onclick="spin()">Change order</button> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul>

Antwort

5

Verwendung nativer JavaScript reverse() Methode

function spin() { 
 
    spinList($('.list-view')); 
 
} 
 

 
var spinList = function($el) { 
 
    $el.each(function() { // iterate over the list 
 
    $(this).append(// appending to change the order 
 
     $('li', this) // get the li element 
 
     .get() // get them as array 
 
     .reverse() // reverse the order 
 
    ) 
 
    }) 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button onclick="spin()">Click</button> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul>

+1

Danke, Das ist gut :) – Shin

+0

@ShijinTR: glad :) –

+1

Sie meine Zeit sparen zu helfen, töten 1 Stunde – Shin

0

Ohne jQuery, mit einigen ES6 Features:

!

function spin() { 
 
    var lists = document.querySelectorAll('.list-view'); 
 
    Array.from(lists).forEach((list) => { 
 
    var reversed = Array.from(list.querySelectorAll('li')).reverse(); 
 
    list.innerHTML = ''; 
 
    var fragment = document.createDocumentFragment(); 
 
    reversed.forEach((item) => { 
 
\t \t \t fragment.appendChild(item);  
 
    }) 
 
    list.appendChild(fragment); 
 
    }) 
 
}
<button onclick="spin()">Change order</button> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul> 
 

 
<ul class="list-view"> 
 
    <li>1</li> 
 
    <li>2</li> 
 
    <li>3</li> 
 
    <li>4</li> 
 
    <li>5</li> 
 
    <li>6</li> 
 
    <li>7</li> 
 
    <li>8</li> 
 
    <li>9</li> 
 
    <li>10</li> 
 
    <li>11</li> 
 
    <li>12</li> 
 
</ul>

Verwandte Themen