使用JQuery更改元素的顺序 [英] Using JQuery to change order of elements

查看:94
本文介绍了使用JQuery更改元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我在做什么错吗?我正在尝试更改某些图像的显示顺序,我希望每次按下按钮时图像都向右/向左移动一个位置.这是我尝试过的,但是没有运气.

Does anybody know what I'm doing wrong? I'm trying to alter the order on which some images show, I want the images to shift to the right/left one spot every time I hit a button. This is what I've tried but no luck.

任何帮助或见识都会得到真正的赞赏

Any help or insight will be truly appreciated

$("#rightShift").click(function(){
    $("img").hide();
    var count = $("img").size();
    $("img").each(function(i) { 
        var indexImg = count - i;
        $("img:eq(indexImg)").show();
    });
}); 
$("#leftShift").click(function(){
    $("img").hide();
    $("img:last").prependTo("img");
    $("img").show();
});

推荐答案

假设您希望它像旋转的旋转木马一样工作(当您右移时,最后一个图像成为第一个图像),那么这就是我的方式做到这一点:

Assuming you want this to work like a carousel that goes around (the last image become the first one when you are shifting right), then this is how I would do it:

$("#rightShift").click(function(){
    $("img:last").detach().insertBefore("img:first");
});

$("#leftShift").click(function(){
    $("img:first").detach().insertAfter("img:last");
});

这篇关于使用JQuery更改元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆