javascript - for 循环迭代 array[i] 仅迭代到最后一个索引?

标签 javascript jquery arrays for-loop pug

我正在使用一个光滑的轮播,我希望非事件幻灯片的不透明度为0.4....所以我创建了一个变量数组(变量引用幻灯片的类)和一个for循环来迭代通过单击 X 类时的 .click() jQuery 函数的变量,在所有其他类上通过 .css 不透明度 0.4。单击时,只有一个类(另一张幻灯片图像)会褪色,因此我循环遍历数组和 console.logged 数组 [i] 和数组。令我惊讶的是,当 console.logging array[i] 时,它记录了带有类和内联样式更改的 html。当我控制台记录数组时,它记录了 [div.test__image.explosiveness] (数组中的最后一个索引)。我的循环出了什么问题/如何针对 css 更改定位所有索引? 我使用 PUG 来处理 HTML 和 JS/jQuery。 提前感谢您的帮助和建议!

.carousel
.carousel__slide
    .test
        .test__image.explosiveness
        p Explosiveness
.carousel__slide
    .test
        .test__image.agility
        p Agility
.carousel__slide
    .test
        .test__image.flexibility
        p Flexibility
.carousel__slide
    .test
        .test__image.balance
        p Balance
.carousel__slide
    .test
        .test__image.footwork
        p Footwork
.carousel__slide
    .test
        .test__image
        p Explosiveness
// carousel fades
var agility = document.getElementsByClassName('agility'),
    explosiveness = document.getElementsByClassName('explosiveness'),
    flexibility = document.getElementsByClassName('flexibility'),
    balance = document.getElementsByClassName('balance'),
    footwork = document.getElementsByClassName('footwork');

var nonFootwork = (agility, balance, flexibility, explosiveness);

$('.footwork').click(function(){
    var len = nonFootwork.length;
    for(var i = 0; i < len; i++){
        $(nonFootwork[i]).css('opacity', '.4');
        console.log(nonFootwork[i]);
        console.log(nonFootwork);
        $('.footwork').css('opacity', '1');
    }
});

最佳答案

您应该使用方括号而不是括号来声明数组:

var nonFootwork = [agility, balance, flexibility, explosiveness];

关于javascript - for 循环迭代 array[i] 仅迭代到最后一个索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174209/

相关文章:

javascript - Ember.js:从 View 中确定当前路线

javascript - 如何将 React-Native 移动应用程序转换为 React 网络应用程序?

javascript - 在 ckeditor 中切换实时预览

javascript - 如何创建嵌套的 json 数据?

ios - Swift 4 中的 UIButton 数组

regex - 如何打印字符串模式的所有实例的接下来的 N 行?

javascript - myfonts 如何跟踪其 webfonts 的页面浏览量?

javascript - jQuery ready() 的替代方法,用于检测 URL 中的页面 anchor 是否发生变化

php - 如何在每个循环中增加 Jquery 变量

javascript - 将数组添加到关联数组/对象