2011-03-24 77 views
0

我一直在这个问题上停留了几天,现在真的让我很沮丧。我正在写一个我认为应该是一个div中的简单无限图像交换,然后调用该类的特定div,以便我可以在同一页面上多次使用它。下面是我使用下面的代码:如何遍历div类中的对象?

HTML:

<div class="project"> 
<div class="project_images"> 
    <img src="../images/portfolio/placeholder.jpg" /> 
    <img src="../images/portfolio/placeholder2.jpg" /> 
    <img src="../images/portfolio/placeholder3.jpg" /> 
    <img src="../images/portfolio/placeholder4.jpg" /> 
</div> 
<div class="project_buttons"> 
    <a href="#" class="prev">PREV</a> 
    <a href="#" class="next">NEXT</a> 
</div> 
</div> 
<div class="project"> 
<div class="project_images"> 
    <img src="../images/portfolio/placeholder.jpg" /> 
    <img src="../images/portfolio/placeholder2.jpg" /> 
    <img src="../images/portfolio/placeholder3.jpg" /> 
    <img src="../images/portfolio/placeholder4.jpg" /> 
</div> 
<div class="project_buttons"> 
    <a href="#" class="prev">PREV</a> 
    <a href="#" class="next">NEXT</a> 
</div> 
</div> 

而jQuery的:

$(document).ready(function(){ 
    next = function(){ 
    var thisFirst = $(this).parents('div').prev('.project_images img:first'); 
    var thisLast = $(this).parents('div').prev('.project_images img:last'); 

    thisLast.after(thisFirst); 
    } 

    prev = function(){ 
    var thisFirst = $(this).parent('div').prev('.project_images img:first'); 
    var thisLast = $(this).parent('div').prev('.project_images img:last'); 

    thisFirst.before(thisLast); 
    } 

    $('.prev').click(function(){ 
    prev(); 
    }); 

    $('.next').click(function(){ 
    next(); 
    }); 
}); 

当点击第一个div类中的 “下一步” 按钮,图像将放入同名的第二个div类(“project_images”)。我明白为什么这样做(:first:last选择器),但我找不出更好的方法来选择对象和/或只选择“project_images”类的一个实例。
任何帮助,将不胜感激......

回答

0
$(document).ready(function(){ 
    next = function(){ 
    var thisFirst = $(this).parents('.project').find('.project_images img:first'); 
    var thisLast = $(this).parents('.project').find('.project_images img:last'); 

    thisLast.after(thisFirst); 
    } 

    prev = function(){ 
    var thisFirst = $(this).parents('.project').find('.project_images img:first'); 
    var thisLast = $(this).parents('.project').find('.project_images img:last'); 

    thisFirst.before(thisLast); 
    } 

    $('.prev').click(prev); 

    $('.next').click(next); 
}); 
+0

你还需要将功能移动到点击事件或$(这)是一个空值。 – Tyde 2011-03-24 12:24:57

+0

这与将函数移入click事件一起工作。谢谢! – dstrunk 2011-03-24 12:31:07