我用纯 CSS/jQuery 制作了一个切换按钮,一切正常。当我复制它并试图切换它时,问题就来了。正如所料,开关同时“切换”,到目前为止,这是我的代码:
HTML
<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
$(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");});
}
});
});
或者 Jsfiddle:
这就是它的工作原理,当您单击切换按钮时,它会检测它是否有一个名为“检测器”的类。如果没有,它会为切换设置动画并创建一个。如果是这样,则意味着该类之前已创建,因此它会以动画方式返回切换并删除该类。
好的,当我复制切换开关时,问题就开始了。我现在有两个我想单独激活。最简单的解决方案是使用 :eq()
jQuery 选择器或 .eq()
jQuery 函数,人们将其归类为更“正确”的选项。
所以我将它添加到第二个切换的代码中,但它没有用。在上面的 fiddle 中,您可以自己测试它。如果有人知道是什么问题,请告诉我,谢谢!
编辑:我已经使用了 :eq()
选择器,但它也没有用。
编辑 2:我使用了一个名为“detector-1”的不同检测器类来防止它干扰另一个检测器。
最佳答案
$(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");
});
}
});
});
使用 this 选择器,您只需定义一次点击处理程序 - 它仍然适用于无数个按钮... "see working fiddle"
忘记说了。我更改了你的 fiddle 中的 css,因为背景图像没有显示,我通过 css 创建了一个白色圆圈......
关于javascript - jQuery 选择器 eq :() not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171494/