【问题标题】:jQuery select an eq target from other list that's not selectedjQuery 从其他未选择的列表中选择一个 eq 目标
【发布时间】:2016-12-07 12:37:33
【问题描述】:

无法让“不”起作用。当我单击列表时,我想使用该索引来定位绝对图像列表。存储在列表中的所选背景图像不会淡出,但其余的会淡出。被选中的人会留下来。

$('.accordion-item').click(function(){
    var index=$(this).index();
    var rotationImage = $('.image-rotation li');
    rotationImage.not('eq(index)').fadeout;
    rotationImage.eq(index).fadeIn();
});

Codepen:http://codepen.io/rezasan/pen/oLPYVr

【问题讨论】:

    标签: javascript jquery html list dom


    【解决方案1】:

    好吧,让我们看看......

    1. 你应该写:eq()不只是eq()
    2. 你必须正确地在:eq()string中转义index
    3. .fadeOut() 而不仅仅是.fadeout

    总的来说,其他一切都很好。 :)

    $('.accordion-item').click(function(){
        var index = $(this).index();
        var rotationImage = $('.image-rotation li');
        rotationImage.not(':eq('+ index +')').fadeOut();
        rotationImage.eq(index).fadeIn();
    });
    

    为了完整起见,您甚至可以在单个链中执行此操作,并且根本不需要 not()

    $('.accordion-item').click(function(){
        $('.image-rotation li').fadeOut().eq($(this).index()).fadeIn();
    });
    

    Working example.更新的代码笔

    【讨论】:

    • 我错过了+index+,是的,它是fadeOut()。非常感谢@eisbehr。你帮了大忙。
    • 不客气,@RezaSan!我刚刚更新了我的答案,向您展示了一种更短的工作方式。
    • 那更短。那么这是否意味着我不必缓存到 li?如果我不先将它们缓存到变量中,我只是担心浏览器会崩溃?你怎么看?
    • 在这种情况下没有区别!链也只获取元素一次,就像变量一样。事实上,我更短的示例应该更快,因为它不使用not() 进行过滤。您可以做的唯一其他事情(但不是必须)是声明一个包含click 事件处理程序外部元素的变量并重用它。 @RezaSan
    • 好的,请注意。再次感谢大师@eisbehr。 *敬礼。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多