events - jQuery and closure -


I have several menus on my page that all use the same mouseover and click on the event, so I It's a function However, for the hover (function, function) function, the Vars always seems to have been assigned to the previous arguments.

  $ (document) .ready (for function () {menuMouseOver = function} (i = 0, u = arguments.length; i & lt; u; i ++) {var Origin = argument [i] .Parent; var active = argument [i]. Active; var childSelect = arguments [i] .childSelect; console.log (active); // logs true active $ (guardian). Hair selection) .No ('.' + Active) .each (function (i, e) {console.log (active);}) / / console_log above provides the correct logging .hor (function () {Console.log (active); // It always logs 2_active $ (this) menu. AddClass (active);}, function () {$ (this) .removeClass (active);});}} Numenes Over ({Parents: '# menu1', active: 'menu1_active', ChildSelect: ': gt (0)'}, {parent: '# menu2', active: 'menu2_active', childSelect: ': gt ( 0) '});});  

Why is it that the last console.log always logs the last active which is [i] logical. Active (In this example, it always logs active of arguments [1]. Active) What am I doing wrong?

In addition, the actual work is more complex, but the problem is also present in this type.

Your problem is that hover event is outside the scope of your execution method If your active variable has already gone through the whole set till the hover is executed and depends on the active status of your last element, then you are seeing this problem because the last log There is an event, which is out of the scope, and there is scope for the other two ends.

Try this:

  

This will actually store the 'Active' value inside the DOM element so that it can be reached within the scope.


Comments