【发布时间】:2016-01-04 01:39:49
【问题描述】:
我正在尝试使用数组索引来允许一组 div ID 在触发 mouseenter 和 mouseleave 函数时从一个 ID 更改为另一个 ID。
我知道还有其他方法可以做到这一点 - 切换、悬停或 CSS 悬停。这只是对我的学习,我很新。
下面的代码被注释了,基本问题与为什么“largeID”(或smallID)的数组变量输出正确的值,但尝试使用索引值却没有。对于每个for语句,我希望鼠标进入div时将smallID[i]值替换为largeID[i]值,但我不想为每个语句编写代码,即“largeID[1] , largeID[2]。
感谢大家的指点!!
$(document).ready(function() {
var smallID = [];
var largeID = [];
var divList = document.getElementsByTagName('div')[1]; //get the second (radialMenu) div in the document
var radialDivList = divList.getElementsByTagName('div'); // get all divs under the second (radialMenu) div
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
alert(smallID[i]); // alerts for smallID / largeID and smallID[i] / largeID[i]
alert(largeID[i]); // give rational and expected output
$('#' + smallID[i]).mouseenter(function () { //works for all four radial menu divs when mouse enters
//BUT largeID[i] is undefined
alert(largeID[i]); // undefined
alert(largeID); // gives expected output of full array
}).mouseleave(function () { //mouseleave function not working
});
}
【问题讨论】:
标签: javascript jquery arrays mouseenter mouseleave