这个问题与我之前在 Using Mouseenter / MouseLeave to Change Div in JavaScript 提出的问题相关.
正如所回答的,我遇到了索引关闭问题。我有一个使用现有索引的新函数,并且提供的答案非常有意义 - 除了一部分。
在此之后:
for (var i = 0; i < radialDivList.length; i++) {
if (!radialDivList[i]) continue; //skip null, undefined and non-existent elements
if (!radialDivList.hasOwnProperty(i)) continue; //skip inherited properties
smallID[i] = radialDivList[i].id; //assign the list of four divs to the smallID array;
largeID[i] = smallID[i] + 'Full'; // add "Full" to each smallID element to make a largeID element
}
我有
$('#' + smallID[i]).mouseenter(function () { //works for all four radial menu divs when mouse enters
alert(largeID[i]); // undefined
alert(largeID); // gives expected output of full array
我现在明白为什么 largeID 索引未定义,但我不明白为什么 mouseenter 函数适用于所有四个径向菜单项。我对索引闭包的理解使我认为,鉴于先前 for 循环的结果,smallID[i].mouseenter 函数的 [i] 值将为“3”。如果是这样,为什么所有四个菜单部分都能正确触发 mouseenter 事件?
感谢您愿意提供的任何帮助!
@programmerGuy - 愿意进一步解释上一个问题吗?
最佳答案
这似乎与浏览器缓存问题有关。重新启动后,此代码不再触发 mouseenter 事件。
关于javascript - 为什么即使索引已关闭,该索引号似乎仍有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34599935/