2011-06-06 68 views
0

没有使用可滚动 - 我试图用'点击'来移动(嵌套)ul的向上或向下。但是在这个例子中:console.log = []?我知道它很丑,我可能不会使用它,但我更感兴趣的是为什么我不能得到'prevList'的结果?Re order list items jquery

$(function(){ 
$('.moveUp').click(function(){ 
    theList = $(this).parent().parent();   
    prevList = $(this).parent().parent().prev(); 
    console.log(prevList);    
     }); 
}); 



<ul> 
<li> 

<div class="category-list"> 
<ul> 
<li>Category: other</li> 
<li>Edit this Category</li> 
<li>Delete this Category</li> 
<li class="moveUp">Move up</li> 
<li class="moveDown">Move down</li> 
</ul> 
</div> 

<div class="category-list"> 
<ul> 
<li>Category: other</li> 
<li>Edit this Category</li> 
<li>Delete this Category</li> 
<li class="moveUp">Move up</li> 
<li class="moveDown">Move down</li> 
</ul> 
</div> 

<div class="category-list"> 
<ul> 
<li>Category: other</li> 
<li>Edit this Category</li> 
<li>Delete this Category</li> 
<li class="moveUp">Move up</li> 
<li class="moveDown">Move down</li> 
</ul> 
</div> 

</li> 

<li>Some other List Item</li> 

<li>Ditto</li> 

</ul> 

回答

1

上面的列表没有prev。其他列表似乎工作正常,控制台显示它获得了以前的div。 prev不环绕,所以如果元素是第一个孩子,那么它不会得到任何东西。

+0

遗憾 - 我知道上面的名单不会得到任何东西!我没有意识到prev不会环绕 - 谢谢 – rebelbass 2011-06-06 03:24:40

0

这工作,如果你给.category列表的div和ULS一个id

$('.moveUp').click(function(){ 

    var theList = $(this).parent(); 
    var theListparentId = $(this).parent().parent().attr('id');  
    var prevList = $(this).parent().parent().prev().children(); 
    var prevListparentId = $(this).parent().parent().prev().attr('id'); 

    if(prevListparentId !== undefined){ 

     $('#'+prevListparentId).html(theList); 
     $('#'+theListparentId).html(prevList); 

     } else { 

     alert('No previous'); 

     }    
    }); 

$('.moveDown').click(function(){ 

    var theList = $(this).parent(); 
    var theListparentId = $(this).parent().parent().attr('id');   
    var nextList = $(this).parent().parent().next().children(); 
    var nextListparentId =$(this).parent().parent().next().attr('id'); 

    if(nextListparentId !== undefined){ 

     $('#'+nextListparentId).html(theList); 
     $('#'+theListparentId).html(nextList); 

    } else { 

     alert('No next'); 

    } 
    }); 
}); 

由于Kingjiv注意到您没有得到结果为“prevList”如果没有一个即prevListparentId !== undefined