悬停数组元素淡化另一个数组中的对应元素[闭包问题]

时间:2021-10-25 21:23:37

What I'm hoping to achieve is when I hover over an element in the deptmts array, the corresponding element in the brnches array is then faded in and out. I've added below what I thought it should be but not really sure where I'm going wrong. Any help would be much appreciated.

我希望实现的是当我将鼠标悬停在deptmts数组中的元素上时,brnches数组中的相应元素随后会淡入淡出。我在下面添加了我认为它应该是但不确定我哪里出错了。任何帮助将非常感激。

var brnches = ["#branch01","#branch02","#branch03","#branch04"]
var deptmts = ["#depart01","#depart02","#depart03","#depart04"]

var brchhov = function() {
    for(var i=0; i<deptmts.length; i++){
        $(deptmts[i]).hover(
            function(){$(brnches[i]).stop(true).fadeTo("fast", 1);},
            function(){$(brnches[i]).stop(true).fadeTo("slow", 0);}
        );
    }
};

1 个解决方案

#1


10  

Classic closure Issue..

经典封闭问题..

var brchhov = function() {
    for(var i=0; i<deptmts.length; i++){
       (function(num){
             $(deptmts[num]).hover(
                 function(){$(brnches[num]).stop(true).fadeTo("fast", 1);},
                 function(){$(brnches[num]).stop(true).fadeTo("slow", 0);}
             );
       })(i);
    }
};

Check Fiddle

#1


10  

Classic closure Issue..

经典封闭问题..

var brchhov = function() {
    for(var i=0; i<deptmts.length; i++){
       (function(num){
             $(deptmts[num]).hover(
                 function(){$(brnches[num]).stop(true).fadeTo("fast", 1);},
                 function(){$(brnches[num]).stop(true).fadeTo("slow", 0);}
             );
       })(i);
    }
};

Check Fiddle