首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery选择器eq:()不工作

jQuery选择器eq:()不工作
EN

Stack Overflow用户
提问于 2014-11-27 13:07:01
回答 2查看 1.3K关注 0票数 0

我用纯CSS/jQuery制作了一个切换按钮,所有操作都很完美。当我复制它并试图切换它时,问题就来了。假设,同时切换“切换”,这是目前为止我的代码:

代码语言:javascript
复制
<div id="container">    
        <div id="switch-1"><div class="light-1"></div><div class="switch"><div class="space"><div class="circle"></div></div></div><div class="light-2"></div></div><br><br>
        <div id="switch-2"><div class="light-1"></div><div class="switch"><div class="space"><div class="circle"></div></div></div><div class="light-2"></div></div>
</div>

jQuery

代码语言:javascript
复制
$(function(){
            $('.space').click(function(){
                if($('.circle').hasClass("detector")){
                    $('.circle').animate({ marginLeft: "2px"}, "slow", function(){$('.light-1').css("background","#8e3135"); $('.light-2').css("background","#adafb2"); $('.circle').removeClass("detector");});
                } else {
                    $('.circle').animate({ marginLeft: "47px"}, "slow", function(){$('.light-1').css("background","#adafb2"); $('.light-2').css("background","#8e3135"); $('.circle').addClass("detector");});
                }
            });

            $('.space').eq(1).click(function(){
                if($('.circle').eq(1).hasClass("detector-1")){
                    $('.circle').eq(1).animate({ marginLeft: "2px"}, "slow", function(){$('.light-1').eq(1).css("background","#8e3135"); $('.light-2').eq(1).css("background","#adafb2"); $('.circle').eq(1).removeClass("detector-1");});
                } else {
                    $('.circle').eq(1).animate({ marginLeft: "47px"}, "slow", function(){$('.light-1').eq(1).css("background","#adafb2"); $('.light-2').eq(1).css("background","#8e3135"); $('.circle').eq(1).addClass("detector-1");});
                }
            });
        });

或者是小提琴:

http://jsfiddle.net/ew0s6nqd/

这就是它的工作方式,当您单击按钮时,它会检测它是否有一个名为“检测器”的类。如果没有的话,它会动画这个按钮并创建一个。如果是这样的话,这意味着类是先前创建的,因此它会将切换回动画并移除类。

好的,当我重复切换时问题就开始了。我现在有两个人,我想单独激活。最简单的解决方案是使用:eq() jQuery选择器或.eq() jQuery函数,人们将其归类为更“正确”的选项。

所以我把它添加到第二个切换的代码中,但是它没有工作。在上面的小提琴里,你可以自己来测试。如果有人知道是哪一个问题,请告诉我,谢谢!

编辑:,我已经使用了:eq()选择器,但也没有工作。

编辑2: -我使用一个不同的检测器类,称为“检测器-1”,以防止它干扰另一个检测器。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-27 13:30:38

代码语言:javascript
复制
$(function () {
    //the click function for every element with the .space class
    $('.space').click(function () {
        //check on the .circle child of the clicked .space using "this"
        if ($('.circle', this).hasClass("detector")) {
            //and animate it
            $('.circle', this).animate({
                marginLeft: "2px"
            }, "slow", function () {
                // since we are in the animate callback, "this" is now the 
                // .circle of the clicked .space
                // we want the lights to change - so we have to travel the dom upwards
                // 1st .parent() brings us to .space
                // 2nd .parent() leads us to .switch
                // siblings() let us find the .light-1 element
                $(this).parent().parent().siblings('.light-1').css("background", "#8e3135");
                // same here for light-2
                $(this).parent().parent().siblings('.light-2').css("background", "#adafb2");
                $(this).removeClass("detector");
            });
        } else {
            $('.circle', this).animate({
                marginLeft: "47px"
            }, "slow", function () {
                $(this).parent().parent().siblings('.light-1').css("background", "#adafb2");
                $(this).parent().parent().siblings('.light-2').css("background", "#8e3135");
                $(this).addClass("detector");
            });
        }
    });
});

使用这个选择器,您只需要定义一次单击处理程序-它仍然可以用于无数个按钮.“见工作小提琴”

忘了提了。我改变了css在你的小提琴,因为背景图像没有显示,我创造了一个白色的圆圈通过css.

票数 2
EN

Stack Overflow用户

发布于 2014-11-27 13:17:20

我想出了如何使它成为感谢@BhushanKawadkwar

我必须在单击函数上使用:eq()选择器,在其他函数中使用.eq()函数。我不知道为什么,但它起作用了,这是工作的小提琴:

http://jsfiddle.net/ew0s6nqd/2/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27171494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档