在for循环中输出javascript数组值会导致未定义

在for循环中输出javascript数组值会导致未定义,javascript,jquery,for-loop,undefined,Javascript,Jquery,For Loop,Undefined,我试图使用一个循环来输出jQuery悬停函数上两个数组的值。在悬停外部,我可以很好地访问这些数组值,但在内部我没有定义 JavaScript对我来说仍然是新的,所以请告诉我我的任何新手错误。谢谢 jQuery(document).ready(function($){ var originalWheelContent = $('.wheel-content').html(), transitionSpeed = 400, segmentTitle = ['collaborative

我试图使用一个循环来输出jQuery悬停函数上两个数组的值。在悬停外部,我可以很好地访问这些数组值,但在内部我没有定义

JavaScript对我来说仍然是新的,所以请告诉我我的任何新手错误。谢谢

jQuery(document).ready(function($){
var originalWheelContent = $('.wheel-content').html(),
    transitionSpeed = 400,
    segmentTitle = ['collaborative','compassionate','inclusive','empowering','responsive'],
    segmentParagraph = [
        'Working together to deliver the best possible outcomes and recognises and values the contribution of others.',
        'Constantly looking for new ways to satisfy the needs of those you work with or care for. Acting with consideration and understanding for others feelings.',
        'Actively seeking and creating opportunities to include others, fostering an environment where everyone feels valued and respected.',
        'Enabling positive change and supporting others to reach their maximum potential.',
        'Reacting positively to change and others needs by being creative and innovative in finding solutions.'
    ];

for(x = 0; x < segmentTitle.length; x++){
    $('.wheel-graphic map area#' + segmentTitle[x]).hover(
        function(){
            $('.wheel-content').fadeOut(transitionSpeed,function(){
                $('.wheel-content').replaceWith('<div class="wheel-content"><h2 class="segment-title">' + segmentTitle[x] + '</h2><h4 class="segment-p">' + segmentParagraph[x] + '</h4></div>');
            });
        }, function() {
            $('.wheel-content').fadeIn(transitionSpeed);
            $('.wheel-content').replaceWith('<div class="wheel-content">' + originalWheelContent + '</div>');
        }
    );
   }
});
jQuery(文档).ready(函数($){
var originalWheelContent=$('.wheel content').html(),
转换速度=400,
segmentTitle=[“协作”、“富有同情心”、“包容”、“授权”、“响应”],
段落=[
“共同努力实现最佳结果,认可并重视他人的贡献。”,
“不断寻找新的方式来满足与你共事或关心的人的需求。考虑他人的感受并理解他人的感受。”,
“积极寻求和创造机会,让他人参与进来,营造一个让每个人都感到受到重视和尊重的环境。”,
“实现积极变革,支持他人发挥最大潜力。”,
“通过创造性和创新性地寻找解决方案,积极应对变化和其他需求。”
];
对于(x=0;x

为了避免
未定义
,您需要确保循环递增变量
x
的范围正确

使用

for (let x = 0; x < segmentTitle.length; x++)
for(设x=0;x
为了避免
未定义
,您需要确保循环递增变量
x
的范围正确

使用

for (let x = 0; x < segmentTitle.length; x++)
for(设x=0;x
在添加我的html时遇到了困难,因此其格式规则很奇怪。希望此图像可以,感谢您考虑我的问题。您确实应该缓存
const$wheelContent=$(“.wheelContent”)
并重用
$wheelContent
。这将极大地优化您的代码执行。添加my html时遇到了困难,因此其格式规则很奇怪。希望此图像可以,感谢您考虑我的问题。您确实应该缓存
const$wheelContent=$(“.wheelContent”)
并重用
$wheelContent
。这将大大优化您的代码执行。这修复了它!谢谢你,鲁宁。我完全没有注意到我没有初始化变量。这就解决了它!谢谢你,鲁宁。我完全没有意识到我没有初始化变量。